Search code examples
androidandroid-listviewandroid-cursoradapter

IllegalStateException “The content of the adapter has changed but ListView did not receive a notification”


I am having ListView with SimpleCursorAdapter

Error I am getting:-

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(2131361944, class android.widget.ListView) with Adapter(class com.pocketpharmacist.adapter.DrugClassesListAdapter)]

When:-

In my Fragment I have enabled the filtering with the adapter, and to filter the List I got one EditText.

Now, 1) when I start entering the text for filter Virtual Keyboard comes alive, and It starts filtering with the list

2)but, the real filtering comes alive when i hides the Keybord. and If i click on listItem with the keybord alive it gives me the above error, which is obvious because data is not reflected to UI

After going in to deep I got to know that for example I want to search ABCDE in a list I have entered ABC in Edittext now when i add D it shows me the filter for ABC and now again if I add E it shows me the filter for ABCD, Buuut Data is originally changed in the background. which is the cause of error.

But I am not able to figure out how to solve this

notifyDataSetChangedhas no effects, please Help me with this, any hint or idea can work

Hear is my code

Method from TextWatcher

public void afterTextChanged(Editable s) {
    searchAdapter.getFilter().filter(s.toString());
    searchAdapter.notifyDataSetChanged();
    drugListView.requestLayout();       
}

Logcat is as below

06-30 18:14:54.299: E/AndroidRuntime(6922): FATAL EXCEPTION: main
06-30 18:14:54.299: E/AndroidRuntime(6922): 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(2131361944, class android.widget.ListView) with Adapter(class com.pocketpharmacist.adapter.DrugClassesListAdapter)]
06-30 18:14:54.299: E/AndroidRuntime(6922):     at android.widget.ListView.layoutChildren(ListView.java:1555)
06-30 18:14:54.299: E/AndroidRuntime(6922):     at android.widget.AbsListView$CheckForTap.run(AbsListView.java:2840)
06-30 18:14:54.299: E/AndroidRuntime(6922):     at android.os.Handler.handleCallback(Handler.java:587)
06-30 18:14:54.299: E/AndroidRuntime(6922):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-30 18:14:54.299: E/AndroidRuntime(6922):     at android.os.Looper.loop(Looper.java:132)
06-30 18:14:54.299: E/AndroidRuntime(6922):     at android.app.ActivityThread.main(ActivityThread.java:4028)
06-30 18:14:54.299: E/AndroidRuntime(6922):     at java.lang.reflect.Method.invokeNative(Native Method)
06-30 18:14:54.299: E/AndroidRuntime(6922):     at java.lang.reflect.Method.invoke(Method.java:491)
06-30 18:14:54.299: E/AndroidRuntime(6922):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
06-30 18:14:54.299: E/AndroidRuntime(6922):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
06-30 18:14:54.299: E/AndroidRuntime(6922):     at dalvik.system.NativeStart.main(Native Method)

Thank you


Solution

  • I have changed the method for filtration instead of writing

    public void afterTextChanged(Editable s) {
        searchAdapter.getFilter().filter(s.toString());
        searchAdapter.notifyDataSetChanged();
        drugListView.requestLayout();       
    }
    

    I have changed it and start using FilterListener and called notifyDataSetChanged in that method and now everything works like charm