1

I have a ListView where I present some option, depending of a data get from database. When an item of that ListView is clicked, it should open an Activity. The listview is, normally, populated with two items. When I click on one of the items, everything goes ok. BUT, if I get out of the Activity, and click the other item, I get this exception:

08-10 13:00:35.724  22877-22877/? E/MessageQueue-JNI﹕ 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. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(2131558531, class android.widget.ListView) with Adapter(class com.infaplic.lpi.Adapters.ListaTributosAdapter)]
        at android.widget.ListView.layoutChildren(ListView.java:1555)
        at android.widget.AbsListView.onTouchUp(AbsListView.java:3617)
        at android.widget.AbsListView.onTouchEvent(AbsListView.java:3429)
        at android.view.View.dispatchTouchEvent(View.java:7837)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2210)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1945)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2072)
        at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1519)
        at android.app.Activity.dispatchTouchEvent(Activity.java:2467)
        at com.infaplic.lpi.activities.SwipeActivity.dispatchTouchEvent(SwipeActivity.java:70)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2020)
        at android.view.View.dispatchPointerEvent(View.java:8017)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3984)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3863)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3423)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3473)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3442)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3549)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3450)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3606)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3423)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3473)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3442)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3450)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3423)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5635)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5615)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5586)
        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5715)
        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:138)
        at android.os.Looper.loop(Looper.java:123)

It is pointing to this line:

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    onSwipeTouchListener.getGestureDetector().onTouchEvent(ev);
    return super.dispatchTouchEvent(ev);//Get the exception here.
}

The ListView is populated this way:

adapter=new ListaTributosAdapter(c, SwipeActivity.beanElementosTributarios);
    listView.setAdapter(adapter);

Where SwipeActivity.beanElementosTributarios is an static array list where the things I want to present in the list view are stored. At this point, there is only one place where the array is modified:

if(SwipeActivity.beanElementosTributarios==null){
                SwipeActivity.beanElementosTributarios=new ArrayList<>();
            }
            SwipeActivity.beanElementosTributarios.add(bean);

you can change the arrayList in another point of the app, but that point of the app is not reached yet. When I debug, I see the arrayList has two items, as it should.

I am going crazy with this exception.

Anybody could help me? (If you need to see other parts of the code, please ask)

Thank you...

EDIT: If I click on the FIRST item of the list, it is presented ok. When I click on the SECOND element, the exception raises. But, if I click on the SECOND element of the list view first, it is presented normally, but if I click in the FIRST element after that, the Exception raises.

EDIT 2: It is not a duplicate of that question. There is no other threads apart of the UI thread, and the arrayList of the adapter is not changed at this point.

EDIT 3:

Found this: https://code.google.com/p/android/issues/detail?id=71936. Maybe a bug?

Fustigador
  • 6,339
  • 12
  • 59
  • 115
  • possible duplicate of [Android, ListView IllegalStateException: "The content of the adapter has changed but ListView did not receive a notification"](http://stackoverflow.com/questions/3132021/android-listview-illegalstateexception-the-content-of-the-adapter-has-changed) –  Aug 10 '15 at 11:29
  • Are you updating the ListView in the background / or non UI thread? – capt.swag Aug 10 '15 at 11:29
  • No, there are no threads apart of the UI thread. – Fustigador Aug 10 '15 at 11:32

1 Answers1

1

You must call notifyDataSetChanged() on your adapter after change data in list. In error says that data in your adapter updated, but view show old data.

Threat70
  • 46
  • 3
  • I know that...First, I get the array and then I set the adapter. The array list is not changed anywhere, at this point. You can change it later, but in the point where the exception is launched, it is not changed. – Fustigador Aug 10 '15 at 11:37
  • When you set the adapter? onStart, onCreate, etc? – Threat70 Aug 10 '15 at 11:41
  • onCreateView(), it is in a Fragment. – Fustigador Aug 10 '15 at 11:42
  • click list item - launch another activity? try to update listview in onResume() – Threat70 Aug 10 '15 at 11:51
  • The Activity is not launched, the exception is thrown when I click the listItem, but I set breakpoints inside the onclick and the execution does not stops there... – Fustigador Aug 10 '15 at 11:53
  • 1
    The activity not launched at second click, but if you click first time - another activity launch, right? and after back to activity with fragment with listview you must update listview – Threat70 Aug 10 '15 at 11:57
  • Dude, in the onPause of the Activity that launches after clicking the first time I was adding a new item to the array...I feel so stupid! Thank for your help... – Fustigador Aug 10 '15 at 12:03