The content of the adapter has changed but ListView did not receive a notification, nothing updated in the background

Magnus Söderlund picture Magnus Söderlund · May 15, 2014 · Viewed 7.4k times · Source

I've read alot about this one on SO but i havent been able to find a solution in my case. Here's the setup:

Activity A, contains the List and the data being displayed. When my data manager's async task finishes updating, the following happens:

protected void onPostExecute(Void v) {
        super.onPostExecute(v);

        if (editedDataSectionRows == null)
            editedDataSectionRows = new int[] {0, 0, 0, 0, 0, 0, 0};

        mData= editedData;
        mSectionRows = editedDataSectionRows;

        if (mCallback != null) {
            mCallback.dataUpdated();
        }
    }

When the data is updated, activity A gets a callback and does the following:

@Override
public void dataUpdated() {
    mData = mDataManagerInstance.getData();
    mDataSectionCount = mDataManagerInstance.getDataSectionRows();
    mListAdapter.notifyDataSetChanged();
}

mData is an ArrayList containing all the "rows" and mDataSectionCount is an int[] containing information about how mData should be split up into sections.

Now, Activity A has an optionsmenu that can open up a second Activity. Activity B.

Every now and then when running Monkey and Activity B finishes. My onResume for Activity A gets called:

@Override
protected void onResume() {
    super.onResume();
    mDataManagerInstance = DataManager.getInstance();

    mDataManagerInstance.setDataUpdatedListener(this);
    mDataManagerInstance.updateData();

    this.invalidateOptionsMenu();
}

(updateData just fires away my AsyncTask if its not already running) And then shit hits the fan... :

FATAL EXCEPTION: main
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.applidium.headerlistview.HeaderListView$InternalListView) with Adapter(class se.*.android.activities.ListActivity$1)]
        at android.widget.ListView.layoutChildren(ListView.java:1582)
        at android.widget.ListView.commonKey(ListView.java:2104)
        at android.widget.ListView.onKeyDown(ListView.java:2085)
        at android.view.KeyEvent.dispatch(KeyEvent.java:2620)
        at android.view.View.dispatchKeyEvent(View.java:7149)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1458)
        at android.widget.ListView.dispatchKeyEvent(ListView.java:2070)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1462)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1462)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1462)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1462)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1462)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1462)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1898)
        at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1375)
        at android.app.Activity.dispatchKeyEvent(Activity.java:2356)
        at com.actionbarsherlock.app.SherlockFragmentActivity.dispatchKeyEvent(SherlockFragmentActivity.java:122)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1825)
        at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3585)
        at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3555)
        at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2805)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:213)
        at android.app.ActivityThread.main(ActivityThread.java:4787)
        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:789)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
        at dalvik.system.NativeStart.main(Native Method)

Any ideas?

Answer

thealeksandr picture thealeksandr · May 15, 2014

try do that in ui thread

runOnUiThread(new Runnable(){
        public void run() {
            mListAdapter.notifyDataSetChanged();
        }
    });