Android for notifyDataSetChanged()获取IllegalStateException错误

时间:2014-12-04 07:57:00

标签: android android-adapter

在这个BaseAdapter中我想通过过滤来刷新列表视图。输入EditText后,IllegalStateException的{​​{1}}错误导致适配器。

logcat的:

notifyDataSetChanged();

我在活动中过滤listView的代码:

FATAL EXCEPTION: main
    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.

我的BaseAdapter:

search_contacts.addTextChangedListener ( new TextWatcher () {
    @Override
    public void afterTextChanged (Editable arg0) {
        final String text = search_contacts.getText ().toString ().toLowerCase ( Locale.getDefault () );
        contactsAdapter.filter ( text );
    }

    @Override
    public void beforeTextChanged (CharSequence arg0, int arg1,int arg2, int arg3) {
    }
    @Override
    public void onTextChanged (CharSequence arg0, int arg1, int arg2,int arg3) {
    }
});

1 个答案:

答案 0 :(得分:0)

如果您在没有调用notifyDataSetChanged()的情况下编辑内容两次,就会发生这种情况。 在下面的行中,您将从适配器中删除所有内容而不通知。

worldpopulationlist.clear();