I am wondering how I should implement a ListAdapter
that loads its views asynchronously into a ListView
? I want to do this because I am populating
You can use an AsyncTask and use the onPostExecute methods to publish the newly loaded results:
private ArrayAdapter adapter = new YourArrayAdapter();
private class YourAsyncTask extends AsyncTask> {
@Override
protected void onPreExecute() {
// start loading animation maybe?
adapter.clear(); // clear "old" entries (optional)
}
@Override
protected List doInBackground(Void... params) {
// everything in here gets executed in a separate thread
return DataBase.getItems();
}
@Override
protected void onPostExecute(List items) {
// stop the loading animation or something
adapter.addAll(items);
}
}