A
A
afna2015-06-13 12:33:40
Java
afna, 2015-06-13 12:33:40

How to resolve IllegalStateException error in ListView + ContentProvider + CursorAdapter + Loader?

I am using ContentProvider + CursorAdapter + Loader.
In a separate thread, I load data from the network, parse it and add it to the database through the ContentProvider:

context.getContentResolver().insert(uri, contentValues);

In the activity, I get Cursor in the onLoadFinish() method:
@Override
public void onLoadFinished(Loader loader, Cursor cursor) {
    cursorAdapter.swapCursor(cursor);
    cursorAdapter.notifyDataSetChanged();
}

If, as I add items, I start scrolling through the list, an error occurs:
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(-1, class com.madyar_android.app.PullToRefresh$4) with Adapter(class com.madyar_android.app.PullToRefresh$PullToRefreshAdapter)]
            at android.widget.ListView.layoutChildren(ListView.java:1548)
            at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:4320)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
            at android.view.Choreographer.doCallbacks(Choreographer.java:579)
            at android.view.Choreographer.doFrame(Choreographer.java:547)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
            at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:153)
            at android.app.ActivityThread.main(ActivityThread.java:5297)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at dalvik.system.NativeStart.main(Native Method)

They write what is solved by calling the cursorAdapter.notifyDataSetChanged() method;
Tell me, please, where should I call notifyDataSetChanged() in this case, or is there a problem somewhere else?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Max, 2015-06-13
@sulik93

They write that

Make sure the content of your adapter is not modified from a background thread, but only from the UI thread

Something is wrong with your streams, smoke.

D
Dmitry Sokolov, 2015-06-13
@retor4i

The information in the adapter was updated (changed), but it did not notify the ListView about it. adapter.notifyDataSetchanged()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question