Listview过滤和适配器

时间:2012-10-06 16:38:47

标签: android listview view filter textwatcher

我有一个ListView,我正在覆盖getView方法,该方法正确填充以下代码。当我使用adapter.getFilter().filter(s)时,如果我使用它会导致代码后显示错误。当我覆盖getView时,我怎么能启用过滤?

lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.activity_list_item,stringArray) 
{   @Override
public View getView(int position, View convertView, ViewGroup parent)
    {View  row= super.getView(position, convertView, parent);
        View row=convertView;
if(position==5){
row = mLayoutInflater.inflate(R.layout.list_header, null);
final TextView sectionView = (TextView) row.findViewById(R.id.list_item_section_text);
sectionView.setText(myList.get(position));
} 
return row;
}
});

myFilter.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {             
adapter.getFilter().filter(s);
});
  
    

ERROR       显示java.lang.NullPointerException       在com.android.testdatabase.Listsort $ 2.onTextChanged(Listsort.java:215)       在android.widget.TextView.sendOnTextChanged(TextView.java:6131)       在android.widget.TextView.handleTextChanged(TextView.java:6172)       在android.widget.TextView $ ChangeWatcher.onTextChanged(TextView.java:6316)       at&gt; 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)     at&gt; android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:583)     at&gt; android.view.inputmethod.BaseInputConnection.setComposingText(BaseInputConnection.java:384>)     at&gt; com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.j&gt; ava:292)     at&gt; com.android.internal.view.IInputConnectionWrapper $ MyHandler.handleMessage(IInputConnection&gt; Wrapper.java:73)     在android.os.Handler.dispatchMessage(Handler.java:99)     在android.os.Looper.loop(Looper.java:123)     在android.app.ActivityThread.main(ActivityThread.java:4627)     at 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(本地方法)

     
    

  

1 个答案:

答案 0 :(得分:0)

您可以在此处引用变量“adapter”:

public void onTextChanged(CharSequence s, int start, int before, int count) {             
adapter.getFilter().filter(s);
});

我没有看到您实际上在任何时候都将该适配器设置为等于您的适配器。我认为你得到一个空指针异常,因为没有设置这个变量。