My expected result is in the first launch there will display a progress dialog to wait for the background thread loading the content. The dialog dismisses after worker thread is done the job. I did search and got this solution How to display progress dialog before starting an activity 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 with the reason that "Only the original thread that created a view hierarchy can touch its views". I suppose there are something blocked the main UI thread, but its still very unclear. So could anyone point me to the problem ? Thanks