Reputation: 2887
I am making a network call, then once the network call is done retrieving the information, I would like to call notifyDataSetChanged. This works if it's all on the same thread, but how would I go about updating the list if the update is happening in another thread?
**
It will throw this error:
android.view.ViewRootImpl$CalledFromWrongThreadException:
Only the original thread that created a view hierarchy can touch its views.
Upvotes: 0
Views: 417
Reputation: 140
You should use a handler. Declare your adapter like an atribute of the class (outside any method). And use this code in your activity:
public Handler updateAdapter = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg)
{
//Update code
}
});
And this one in your thread (to call the handler):
updateCode.sendEmptyMessage(0);
Upvotes: 1
Reputation: 1306
You can use AsyncTask to perform your networking operation on a separate thread. You start the AsyncTask from your UI thread and the task itself will run on a separate thread. When the AsyncTask is done, AsyncTask's onPostExecute method will run on the UI thread again from which you can call the notifyDataSetChanged() method.
Here's some useful info on AsyncTask in the Android developer docs
Upvotes: 1