I have a application already working fine. I need to implement a quick search feature to it. Quick search i mean as the users types in every single character i 开发者_StackOverflow社区want to result to be fetched. My Activity is a list activity for which the data is coming from the database query. Say the list view has 50 items, and when the user searches with word as "test" I want to query the database and filter the items and display it in the same list view. Something like the contact search in android. Please let me know how to implement this. A quick sample would be help full. Thank you.
you can do by this
I implemented this feature in one of my previous app. The entire code is too long to be pasted here. So, I have posted some portions of it. You may read a bit about the approach used by me and get it done for your app.
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());
};
};
精彩评论