安卓是否有可能为ListView设置搜索栏,以便在搜索栏被触摸时会弹出一个键盘,当在搜索栏中输入文本时,会显示ListView中匹配的项目?android - 搜索listview?
我真正需要的是带有键盘的搜索栏。
更新:
我已经添加,带来了一个键盘,我可以键入的EditText字段的EditText场。我想要的是ListView中显示的列表中项目的前几个字符与输入到EditText窗口中的字符匹配。
我试过按照这里列出的方法ListView Filter,但我有点困惑,有多少过滤已经在ListView中完成?
1)我是否需要创建一个单独的数组,用于存储与输入到EditText中的文本相匹配的值?从这篇文章Call adapter.notifyDataSetChanged看来,ListView已经有了一个阴影数组来做到这一点,并且它在adapter.notifyDataSetChanged()时被更新。叫做。
2)我需要调用adapter.notifyDataSetChanged();在EditText窗口中键入一些文本后ListView被更新了吗?
3)我是否需要扩展ListActivity,因为此post表示?如果是的话,如果活动类已经从主要活动扩展,我该如何扩展我的活动类?
4)我现在有如下:
ArrayAdapter<String> adapter = null;
private EditText filterText = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.symptom);
ListView symptomList = (ListView) findViewById(R.id.ListView_Symptom);
symptomList.setTextFilterEnabled(true);
symptomList.setFastScrollEnabled(true);
filterText = (EditText) findViewById(R.id.search_box);
filterText.addTextChangedListener(filterTextWatcher);
adapter = new ArrayAdapter<String>(this, R.layout.menu_item, symptomsArray);
symptomList.setAdapter(adapter);
private TextWatcher filterTextWatcher = 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);
adapter.notifyDataSetChanged();
}
};
不幸的是在那一刻,我在的EditText框中键入,我在
Thread [<7> Filter] (Suspended (exception NullPointerException))
ArrayAdapter$ArrayFilter.performFiltering(CharSequence) line: 437
Filter$RequestHandler.handleMessage(Message) line: 234
Filter$RequestHandler(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 123
HandlerThread.run() line: 60
任何想法得到一个空指针异常是什么我我错过了?
我遵循你的建议,并用我使用的代码更新了我的问题。任何想法我的代码中缺少什么? – 2010-08-30 02:47:28
Android源代码中的ArrayAdapter实现了Filterable:http://android.git.kernel.org/?p=platform/frameworks/base。GIT中; A =斑点; F =芯/ JAVA /机器人/插件/ ArrayAdapter.java; H = 32e55048ed18734c71c95b7f9a4e82283a7bf6c1; HB =参/头/升级Froyo – noah 2010-09-07 19:40:09