How to text filter an Android ListView backed by a SimpleCursorAdapter?

前端 未结 3 948
执笔经年
执笔经年 2020-12-05 01:11

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

相关标签:
3条回答
  • 2020-12-05 01:22

    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:

    • How to dynamically update a ListView on Android
    0 讨论(0)
  • 2020-12-05 01:35

    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.

    0 讨论(0)
  • 2020-12-05 01:42

    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.

    0 讨论(0)
提交回复
热议问题