我正在尝试ListView
在 android 应用程序中使用过滤器,但没有这样做。到目前为止,这是我的代码:
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
mHistoryAdapter.getFilter().filter(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
@Override
public Filter getFilter() {
// TODO Auto-generated method stub
if (planetFilter == null)
planetFilter = new PlanetFilter();
return null;
}
private class PlanetFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint == null || constraint.length() == 0) {
results.values = arraylist;
results.count = arraylist.size();
} else {
ArrayList<HashMap<String,Object>> arraylistmenu
= new ArrayList<HashMap<String,Object>>();
String filterString = constraint.toString().toLowerCase();
HashMap<String, Object> filterableString;
for(int i = 0; i<arraylist.size(); i++){
filterableString = arraylist.get(i);
if(filterableString.toString().contains(filterString)){
arraylistmenu.add(filterableString);
}
}
results.values = arraylistmenu;
results.count = arraylistmenu.size();
}
return results;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
}
错误堆栈跟踪:
07-22 16:22:56.617: E/AndroidRuntime(359): FATAL EXCEPTION: main
07-22 16:22:56.617: E/AndroidRuntime(359): java.lang.NullPointerException
07-22 16:22:56.617: E/AndroidRuntime(359): at com.app.example$2.onTextChanged(example.java:117)
07-22 16:22:56.617: E/AndroidRuntime(359): at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
07-22 16:22:56.617: E/AndroidRuntime(359): at android.widget.TextView.handleTextChanged(TextView.java:6172)
07-22 16:22:56.617: E/AndroidRuntime(359): at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:6316)
07-22 16:22:56.617: E/AndroidRuntime(359): at android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:889)
07-22 16:22:56.617: E/AndroidRuntime(359): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:352)
07-22 16:22:56.617: E/AndroidRuntime(359): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:269)
07-22 16:22:56.617: E/AndroidRuntime(359): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:432)
07-22 16:22:56.617: E/AndroidRuntime(359): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:409)
07-22 16:22:56.617: E/AndroidRuntime(359): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:28)
07-22 16:22:56.617: E/AndroidRuntime(359): at android.text.method.QwertyKeyListener.onKeyDown(QwertyKeyListener.java:195)
07-22 16:22:56.617: E/AndroidRuntime(359): at android.text.method.TextKeyListener.onKeyDown(TextKeyListener.java:132)
07-22 16:22:56.617: E/AndroidRuntime(359): at android.widget.TextView.doKeyDown(TextView.java:4304)
07-22 16:22:56.617: E/AndroidRuntime(359): at android.widget.TextView.onKeyDown(TextView.java:4149)
07-22 16:22:56.617: E/AndroidRuntime(359): at android.view.KeyEvent.dispatch(KeyEvent.java:1037)
07-22 16:22:56.617: E/AndroidRuntime(359): at android.view.View.dispatchKeyEvent(View.java:3740)
07-22 16:22:56.617: E/AndroidRuntime(359): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
07-22 16:22:56.617: E/AndroidRuntime(359): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
07-22 16:22:56.617: E/AndroidRuntime(359): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
07-22 16:22:56.617: E/AndroidRuntime(359): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1667)
07-22 16:22:56.617: E/AndroidRuntime(359): at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1102)
07-22 16:22:56.617: E/AndroidRuntime(359): at android.app.Activity.dispatchKeyEvent(Activity.java:2063)
07-22 16:22:56.617: E/AndroidRuntime(359): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1643)
07-22 16:22:56.617: E/AndroidRuntime(359): at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2471)
07-22 16:22:56.617: E/AndroidRuntime(359): at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2441)
07-22 16:22:56.617: E/AndroidRuntime(359): at android.view.ViewRoot.handleMessage(ViewRoot.java:1735)
07-22 16:22:56.617: E/AndroidRuntime(359): at android.os.Handler.dispatchMessage(Handler.java:99)
07-22 16:22:56.617: E/AndroidRuntime(359): at android.os.Looper.loop(Looper.java:123)
07-22 16:22:56.617: E/AndroidRuntime(359): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-22 16:22:56.617: E/AndroidRuntime(359): at java.lang.reflect.Method.invokeNative(Native Method)
07-22 16:22:56.617: E/AndroidRuntime(359): at java.lang.reflect.Method.invoke(Method.java:521)
07-22 16:22:56.617: E/AndroidRuntime(359): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-22 16:22:56.617: E/AndroidRuntime(359): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-22 16:22:56.617: E/AndroidRuntime(359): at dalvik.system.NativeStart.main(Native Method)