我有一个应用程序已经运行良好。我需要对其实施快速搜索功能。快速搜索我的意思是当用户输入我想要获取的每个字符时。我的活动是一个列表活动,其数据来自数据库查询。假设列表视图有 50 个项目,当用户使用单词作为“测试”进行搜索时,我想查询数据库并过滤项目并将其显示在同一个列表视图中。类似于 android 中的联系人搜索。请让我知道如何实现这一点。一个快速的样本将有助于充分。谢谢你。
2 回答
0
于 2011-07-04T07:03:18.983 回答
0
我在我以前的一个应用程序中实现了这个功能。整个代码太长,这里就不贴了。所以,我已经发布了它的一些部分。您可能会阅读一些关于我使用的方法的信息,并为您的应用程序完成它。
EditText filterText;
words = new MySimpleCursorAdapter(Main.this, R.layout.emptylist, temp, from, to);
filterText= (EditText) findViewById(R.id.search_box);
filterText.addTextChangedListener(filterTextWatcher);
words.setFilterQueryProvider(new FilterQueryProvider() {
@Override
public Cursor runQuery(CharSequence constraint) {
Cursor cur=mDbHelper.fetchSugNotes(filterText.getText().toString());
return cur;
}
});
private TextWatcher filterTextWatcher = new TextWatcher() {
public void afterTextChanged(android.text.Editable s) {
};
public void beforeTextChanged(CharSequence s, int start, int count, int after) {};
public void onTextChanged(CharSequence s, int start, int before, int count) {
words.getFilter().filter(s.toString());
};
};
于 2012-11-18T17:13:48.150 回答