Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to text filter an Android ListView backed by 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?

like image 412
CodeFusionMobile Avatar asked Jan 04 '10 22:01

CodeFusionMobile


1 Answers

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) {
  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.

like image 59
radhoo Avatar answered Nov 15 '22 17:11

radhoo