安卓是否有可能爲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