适配器过滤抛出异常

时间:2011-12-09 10:18:47

标签: android

CODE

private TextWatcher quickSearchTextWatcher = new TextWatcher() {
  @Override
  public void onTextChanged(CharSequence s, int start, int before,
   int count) {

  }

  @Override
  public void beforeTextChanged(CharSequence s, int start, int count,
    int after) {

  }

  @Override
  public void afterTextChanged(Editable s) {
   try {
    filter();
   } catch (Exception e) {

   }
  }
 };


private void filter() {
  String str = activitySecondBrandTextQuickSearch.getText().toString()
    .trim();

  if (str.length() > 2) {
   return;
  }

  try {
   ArrayList<Brand> companyArrayList = new ArrayList<Brand>();

   for (Brand c : ApplicationConstants.brandArrayList) {
    if (c.getName().toLowerCase().startsWith(str)) {
     companyArrayList.add(c);
    }
   }

   companyAdapter = null;
   brandListView.setAdapter(null);

   Collections.sort(companyArrayList, Brand.NAME_ORDER);
   companyAdapter = new BrandAdapter(SecondScreenBrandsActivity.this,
     companyArrayList);
   brandListView.setAdapter(companyAdapter);
   brandListView.setTextFilterEnabled(true);
  } catch (IllegalStateException ise) {
   companyAdapter = null;
   brandListView.setAdapter(null);
  } catch (Exception e) {
   Log.e(TAG, "TextWatcher->" + e.getMessage());
  }
 }

ERROR

java.lang.IllegalStateException: You cannot call onTextChanged with a non filterable adapter
at android.widget.AbsListView.onTextChanged(AbsListView.java:3625)
android.widget.TextView.sendOnTextChanged(TextView.java:6131)
android.widget.TextView.handleTextChanged(TextView.java:6172)
android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:6316)
android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:889)
android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:352)
android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:269)
android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:432)
android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:409)
android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:28)
android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:583)
android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:174)
com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:120)
android.view.inputmethod.InputConnectionWrapper.commitText(InputConnectionWrapper.java:75)
com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:247)
com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:73)
android.os.Handler.dispatchMessage(Handler.java:99)
android.os.Looper.loop(Looper.java:123)
android.app.ActivityThread.main(ActivityThread.java:4627)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:521)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
dalvik.system.NativeStart.main(Native Method)

这里的问题是错误信息没有显示异常发生或行号等。我需要一些帮助,因为我找不到问题所在。每当我尝试写超过3个字符的东西时,应用程序都会崩溃。有什么想法吗?

3 个答案:

答案 0 :(得分:0)

BrandAdapter是否实施Filterable

答案 1 :(得分:0)

我遇到了类似的问题并发现了问题的原因,在某些情况下我的文字改变了听众被添加 TWICE
由于侦听器的第一个实例更改了EditText内容,因此第二个实例会导致致命异常。

答案 2 :(得分:0)

问题是由TextChangedListener添加两次引起的。 如果您有自己的标题视图,则需要在TextFilter上停用ListAdapter

protected void configureList(Activity activity, final ListView listView) {
    super.configureList(activity, listView);

    listView.setFastScrollEnabled(true);
    listView.setDividerHeight(0);
    listView.setTextFilterEnabled(false);


    View headerView = activity.getLayoutInflater().inflate(R.layout.outlet_list_item_labels, null);
    EditText search = butterknife.Views.findById(headerView, R.id.et_manage_outlets_search);
    if(!search.hasOnClickListeners()) {
        search.addTextChangedListener(new DelayedTextWatcher(1000) {

            @Override
            public void afterTextChangedDelayed(Editable s) {
                str_search = s.toString();
                refresh();
            }
        });
    }
    getListAdapter().addHeader(headerView);
}