Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If data for ListAdapter is collected in doInBackground() of AsyncTask, Activity onCreate() does not wait for AsyncTask to complete.</p> <p>So what we have: 1. Activity Start 2. AsyncTask start 3. Activity trying to set ListAdapter - it's empty, because AsyncTask does not completed 4. AsyncTask completed - but ListAdapter filled with empty data</p> <p>My solution: AsyncTask should notify ListAdapter of Activity after data loading completes:</p> <pre><code>Public class MyActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.myActivity); ExpandableListView listView = (ExpandableListView) findViewById(R.id.expList); //MyArrayList - data source for your list. For example, I use static ArrayList from other class. Replace with your data object. final MyExpandableListAdapter adapter = new MyrExpandableListAdapter(getApplicationContext(), myArrayList); listView.setAdapter(adapter); int taskId=1; // type of task should be executed MyAsyncTask myTask = new MyAsyncTask(taskId); myTask.execute(adapter); @Override protected Dialog onCreateDialog(int id) { ProgressDialog progress = null; progress = new ProgressDialog(this); </code></pre> <p>//Should user work with app while data loading? In my case, no, so setCancelable=false progress.setCancelable(false);</p> <pre><code> if (id==1) { progress.setMessage("Getting locations, please wait..."); } if (id==2) { progress.setMessage("Task type #2 performing..."); } return progress; } // end onCreateDialog class MyAsyncTask extends AsyncTask&lt;Void, Void, Void&gt; { MyExpandableListAdapter adapter; //ExpandableListAdapter you wanna notify about data load completion int taskId; //Type of tasks in your Activity. You have only one, however - getLocations(). MyAsyncTask(int taskId) { //save task ID to use in doInBackground, showDialog and dismissDialog this.taskId=taskId; } @Override protected Void doInBackground(Void... params) { if (taskId==1) { // YOUR LONG-TIME TASK #1 THAT UPDATES DATA SOURCE FOR LIST ADAPTER GOES HERE; } if (taskId==2) { // YOUR LONG-TIME TASK #2 THAT UPDATES DATA SOURCE FOR LIST ADAPTER GOES HERE; // create more task types if needed } //this adapter will be used in onPostExecute() to notify Activity about data changes adapter = (MyExpandableListAdapter) params[0]; return null; } @Override protected void onPreExecute() { //Show dialog for this task type. if need more than one - us switch here and in onCreateDialog, // different dialog types for different tasks etc. showDialog(taskId); } @Override protected void onPostExecute(Void result) { // IMPORTANT! Update data in Activity adapter.notifyDataSetChanged(); dismissDialog(taskId); } } //end of AsyncTask </code></pre> <p>} //end of Activity class</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload