android listView error.... Adapter should not be set from background thread....!
I am working on tabHost with 4 tabwidget, my all 开发者_如何学JAVAfour tab widget has a listActivity that shows list of items from different arraylist objects set from Json parsing Bean classes..
Now the application is working f9 but,,, about 5 in 1 time ratio it shows exception that my adapter is reset but listView unable to display data as the adapter is set from background thread.
I can't provide the adapter data from same UI thread b'coz my Bean classes and Data manager are defined else where....
I have used adapter.notifyDataSetChanged() where required...
Please do not suggest that...With Regards,
Arpit
Even if things are defined some place else you should still be able to set the adapter using:
runOnUiThread(new Runnable() {
@Override
public void run() {
list.setAdapter(adapter);
}
});
or simply post a runnable to the UI thread:
view.post(new Runnable() {
@Override
public void run() {
list.setAdapter(adapter);
}
});
Hope this helps, Christoffer
I'd recommend using a Handler
(http://developer.android.com/reference/android/os/Handler.html) to perform all changes to the adapter: create a Handler instance for each Activity and then send a Message
(http://developer.android.com/reference/android/os/Message.html) to that handler from your data manager/bean classes with the new data for the list adapter. You can then update the adapter safely from the Handler as it will perform its work on the UI thread.
Short writeup is here: http://www.tutorialforandroid.com/2009/01/using-handler-in-android.html and there are a number of other questions on SO that describe it.
精彩评论