确保您的适配器的内容不是从后台线程修改,但只能从UI线程 [英] Make sure the content of your adapter is not modified from a background thread, but only from the UI thread

查看:389
本文介绍了确保您的适配器的内容不是从后台线程修改,但只能从UI线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用的EditText一个活动(名为filtro)和一个ListView(名为list_view)。
当用户插入文本,适配器应该刷新;
如果我改变文字很多次,涉及崩溃之后消息请确保您的适配器的内容不是从后台线程修改,但只能从UI线程

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"

听者

    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实现过滤的{
    [...]

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

错误日志:

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
与对照如果(Looper.myLooper()== Looper.getMainLooper())我已经明白法performFiltering()自动生成一个新的线程。

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

我创建了一个名为新方法FILTRA()为迫使code使用MainTread:

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...]
                    }

}

推荐答案

您ListView和EDITTEXT都在相同的布局?
如果EDITTEXT是在对话框或东西,它可能是你改变了这一切是影响从不同的线程列表中的文本。
包装你更新到适配器:

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

这篇关于确保您的适配器的内容不是从后台线程修改,但只能从UI线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆