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

11,786

your listView and editText are on the same layout? if the editText is on a dialog or something, it's possible that you are changing the text that is affecting the list from a different thread. wrap your update to the adapter with:

runOnUiThread(new Runnable() {
public void run() {
    adapter...
}
});
Share:
11,786

Related videos on Youtube

Tirrel
Author by

Tirrel

Hello world rulez

Updated on June 30, 2022

Comments

  • Tirrel
    Tirrel almost 2 years

    I've an Activity with a EditText (named "filtro") and a ListView (named "list_view"). When the user inserts text, the adapter should be refresh; if i change text many times, comes a crash followed by the message "Make sure the content of your adapter is not modified from a background thread, but only from the UI thread"

    listener :

        filtro.addTextChangedListener(new TextWatcher() {
    
            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
    
                Log.d(TAG, "onTextChanged(" + cs + ")");
                adapter.getFilter().filter(cs); 
            }
            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }           
            @Override
            public void afterTextChanged(Editable arg0) {
            }
        });     
    }
    

    adapter:

    public class ArticoliAdapter extends BaseAdapter implements Filterable { [...]

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        [...]
    }
    
    @Override
    public Filter getFilter() {
    
        if(mFilter == null)
            mFilter = new CustomFilter();
        return mFilter;
    }
    
    
    
    
    private class CustomFilter extends Filter {
    
    
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
    
            FilterResults results = new FilterResults();
    
            if (index == null) {
                synchronized (mLock) {
                    index = new ArrayList<Long>();
                }
            } 
            else {
                index.clear();
            }
    
    
            [...some controls...] 
    
                if(flag_controls) {                     
                    index.add(new Long(i));
                    categoria_vuota = false;
                    continue;
                }
            }
    
            filtered = true;
    
            synchronized (mLock) {
                results.values = index;
                results.count = index.size();
            }            
            return results;            
        }
    
    
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
    
            notifyDataSetChanged();
    
            if (results.count == 0)
                notifyDataSetInvalidated();
            else {
                index = (ArrayList<Long>) results.values;
                notifyDataSetChanged();
            }
        }
    } 
    

    ErrorLog:

    12-03 16:53:26.527: D/AndroidRuntime(15434): Shutting down VM
    12-03 16:53:26.527: W/dalvikvm(15434): threadid=1: thread exiting with uncaught exception (group=0x416052a0)
    12-03 16:53:26.543: E/AndroidRuntime(15434): FATAL EXCEPTION: main
    12-03 16:53:26.543: E/AndroidRuntime(15434): 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(2131230967, class android.widget.ListView) with Adapter(class it.losi.ordcliagent.ArticoliAdapter)]
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at android.widget.ListView.layoutChildren(ListView.java:1538)
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at android.widget.AbsListView.onLayout(AbsListView.java:2300)
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at android.view.View.layout(View.java:14061)
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at android.view.ViewGroup.layout(ViewGroup.java:4374)
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at android.view.View.layout(View.java:14061)
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at android.view.ViewGroup.layout(ViewGroup.java:4374)
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at android.view.View.layout(View.java:14061)
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at android.view.ViewGroup.layout(ViewGroup.java:4374)
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at android.view.View.layout(View.java:14061)
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at android.view.ViewGroup.layout(ViewGroup.java:4374)
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at android.view.View.layout(View.java:14061)
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at android.view.ViewGroup.layout(ViewGroup.java:4374)
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1986)
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1807)
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4464)
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at android.view.Choreographer.doFrame(Choreographer.java:525)
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at android.os.Handler.handleCallback(Handler.java:615)
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at android.os.Handler.dispatchMessage(Handler.java:92)
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at android.os.Looper.loop(Looper.java:137)
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at android.app.ActivityThread.main(ActivityThread.java:4895)
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at java.lang.reflect.Method.invokeNative(Native Method)
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at java.lang.reflect.Method.invoke(Method.java:511)
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
    12-03 16:53:26.543: E/AndroidRuntime(15434):    at dalvik.system.NativeStart.main(Native Method)
    

    SOLUTION With the control if(Looper.myLooper() == Looper.getMainLooper()) i've understand that the method performFiltering() generate automatically a new thread.

    I created a new method called "filtra()" for forcing the code to use the MainTread:

    public class CustomFilter extends Filter {
    
    
        public void filtra(final CharSequence constraint) {
    
            activity.runOnUiThread(new Runnable() {
                public void run() {
                    publishResults(constraint, eseguiAggiornamento(constraint));
                }});
        }
    
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
    
            return eseguiAggiornamento(constraint);         
        }
    
        private FilterResults eseguiAggiornamento(CharSequence constraint) {
            [...code...]
                        }
    

    }