ListView IllegalStateException: “The content of the adapter has changed but ListView did not receive a notification”
This was discussed earlier here Android, ListView IllegalStateException: "The content of the adapter has changed but ListView did not receive a notification" but I still have the problem. I've got Service, which dowloads data from web, saves it do db and fires intent when it's completed. My Activity has ListView and BroadCast receiver:
private class NewsUpdateReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
mNotificationManager.cancel(NewsService.NOTIFICATION_ID);
loadNewsFromDB();
}
}
private void loadNewsFromDB(){
mList.clear();
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(MeidahonProvider.CONTENT_URI, null, null, null, null);
if(cursor.moveToFirst()){
do{
String title = cursor.getString(MeidahonProvider.TITLE_COLUMN);
String description = cursor.getString(MeidahonProvider.DESCRIPTION_COLUMN);
String link = cursor.getString(MeidahonProvider.LINK_COLUMN);
long datems = cursor.getLong(MeidahonProvider.DATE_COLUMN);
Date date=new Date(datems);
NewsItem item = new NewsItem(title, description, link, date);
mList.add(item);
mAdapter.notifyDataSetChanged();
}while(cursor.moveToNext());
}
cursor.close();
}
Every time when item is add to ArrayList, I call mAdapter.notifyDataSetChanged();
but still have exception. How can I solve the problem?
UPD LOG
java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(2131296334, class android.widget.ListView) with Adapter(class com.transportoid.Tracks.TrackListAdapter)]
at android.widget.ListView.layoutChildren(ListView.java:1432)
at android.widget.AbsListView.onTouchEvent(Ab开发者_如何学运维sListView.java:2062)
at android.widget.ListView.onTouchEvent(ListView.java:3234)
at android.view.View.dispatchTouchEvent(View.java:3709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:852)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
First off, unless you "need" for your various bounded components to be made aware of the change in the adapter content on every iteration I would only call adapter.notifyDataSetChanged() after you've completed all the additions.
Secondly, can you provide the Exception and StackTrace?
Third, what Thread is this code being invoked with (the main GUI dispatching thread or a background)?
Thanks Migher for the updated Logcat. Do me a favor to help clarify a point: can you run a quick test to see what happens when you manually enter the same data values ... do it from a menu selection or a button click event (so we'll know it's on the GUI thread)? Getting the same error?
Also worth pointing out is give a SimpleCursorAdapter a try instead of the regular Adapter you're currently using. As I'm not able to run this through my debugger this might be just a simpler fix than going back and forth with "asynchronous tech support" suggestions, as I'm sure you'd like this fixed sooner rather than later.
Let me know how either of these work - thanks
精彩评论