确保不从后台线程修改适配器的内容,而只是从UI线程修改

时间:2013-12-03 15:44:54

标签: android multithreading listview filter adapter

我有一个带有EditText的活动(名为“filtro”)和一个ListView(名为“list_view”)。 当用户插入文本时,应该刷新适配器; 如果我多次更改文本,则会出现崩溃,然后显示消息“确保未从后台线程修改适配器的内容,但只能从UI线程修改”

听者 :

    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) {
        }
    });     
}

适配器:

公共类ArticoliAdapter扩展BaseAdapter实现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();
        }
    }
} 

错误日志:

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)

使用控件if(Looper.myLooper()== Looper.getMainLooper())我明白方法performFiltering()会自动生成一个新线程。

我创建了一个名为“filtra()”的新方法,用于强制代码使用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...]
                    }

}

1 个答案:

答案 0 :(得分:6)

你的listView和editText在同一个布局上? 如果editText位于对话框或其他内容上,则可能是您正在从其他线程更改影响列表的文本。 使用以下命令将更新包装到适配器:

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