Sie sind auf Seite 1von 2

database - Android: How to text filter a listview based on a simplecurs...

http://stackoverflow.com/questions/2002607/android-how-to-text-filter-...

Android: How to text filter a listview based on a simplecursoradapter?

I have a ListView that is backed by a SimpleCursorAdapter. I'd like to be able to filter the list like you would a contacts list, just by typing, and I came across the textFilterEnabled() Problem is, I couldn't see how to get it to work with a SimpleCursorAdapter. Is this even possible? If so, how is it done?
database android listview filter cursor

asked Jan 4 '10 at 22:06 CodeFusionMobile 3,306 3 22 50 63% accept rate feedback

3 Answers
The setTextFilterEnabled() method doesn't automatically implement filtering, as it doesn't know what in your Cursor the text should be filtered against. This android-developers thread has more details. Actually, there was a good question asked the other day, which actually is very similar to your question; though it originally was asking how to handle filtering when there is no physical keyboard on a device: http://stackoverflow.com/questions/1737009/answer-to-making-a-nice-looking-listview-filteron-android
answered Jan 5 '10 at 4:04 Christopher 26.9k 2 39 56 feedback

For a SimpleCursorAdapter cursor, you only need to use the setFilterQueryProvider, to run another query for your cursor, based on the constraint: m_Adapter.setFilterQueryProvider(new FilterQueryProvider() { public Cursor runQuery(CharSequence constraint) { Log.d(LOG_TAG, "runQuery constraint:"+constraint); //uri, projection, and sortOrder might be the same as previous //but you might want a new selection, based on your filter content (constraint Cursor cur = managedQuery(uri, projection, selection, selectionArgs, sortOrder return cur; //now your adapter will have the new filtered content } }); When a constraint is added (eg. by using a TextView) the adapter must be filtered: public void onTextChanged(CharSequence s, int start, int before, int count) {

1 de 2

05/03/2012 11:57

database - Android: How to text filter a listview based on a simplecurs...

http://stackoverflow.com/questions/2002607/android-how-to-text-filter-...

Log.d(LOG_TAG, "Filter:"+s); if (m_slvAdapter!=null) { m_Adapter.getFilter().filter(s); } } Hope this helps. I will try to write a complete article , with source code the next few days.
edited Feb 21 '11 at 15:50 mbaird 13.4k 3 32 49 answered Feb 21 '11 at 15:49 radhoo 192 1 7

Good answer, one of the few that show the runQuery part with cursor - would be even better if you included example of the selection. E.g. selection = FIELD_NAME_TO_FILTER + " like '" + constraint.toString() + "'"; DEzra Nov 22 '11 at 11:15 Thank you . Here is a sample with selection: pocketmagic.net/?p=1870 radhoo Jan 13 at 15:18 How should we call a cursor in runQuery if we are using a SQLite database? Using the same databaseHelper instance as the one used for creating the cursor for SimpleCursorAdapter is giving me a "Invalid statement in fillWindow() " error. movingahead Feb 24 at 13:43

Was this post useful to you?

Yes

No

i found this article helpful http://androidcookbook.oreilly.com /Recipe.seam;jsessionid=CE37400B3E545937B70BE2E9F94E78BB?recipeId=404 basically, you setTextFilterEnabled(true) on your listview, and you use setStringConversionColumn() and setFilterQueryProvider() on your SimpleCursorAdapter .
answered Nov 30 '10 at 17:43 Ben H 473 2 10 feedback

Not the answer you're looking for? Browse other questions tagged database
android listview filter cursor or ask your own question.

question feed

2 de 2

05/03/2012 11:57

Das könnte Ihnen auch gefallen