My expected result in the first run will display a progress dialog to wait for the background content stream to load. The dialog is rejected after the workflow has completed the task. I did a search and got this solution. How to display a progress dialog before starting work in Android?
This is my complete code:
private ManagerApplication app;
private ImageAdapter adapter;
private GridView gridview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setupView();
}
private void setupView() {
gridview = (GridView) findViewById(R.id.gridview);
app = (ManagerApplication) getApplication();
ProgressDialog progress = new ProgressDialog(this);
progress.setMessage("Loading...");
new LoadImageTask(progress, this).execute();
}
private class LoadImageTask extends AsyncTask<Void, Void, Void> {
private ProgressDialog progress;
private Context context;
public LoadImageTask(ProgressDialog progress, Context context) {
this.progress = progress;
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progress.show();
}
@Override
protected Void doInBackground(Void... params) {
adapter = new ImageAdapter(app.getShoes(), context);
gridview.setAdapter(adapter);
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
progress.dismiss();
}
}
However, my application crashes because "Only the original thread that created the hierarchy of views can touch its views." I suppose something blocked the main UI thread, but its still very obscure. So can anyone point me to a problem? Thanks
Harry source
share