18

我有一个由 SimpleCursorAdapter 支持的 ListView。

我希望能够像过滤联系人列表一样过滤列表,只需键入即可,我遇到了 textFilterEnabled()

问题是,我看不出如何让它与 SimpleCursorAdapter 一起工作。

这甚至可能吗?
如果是这样,它是如何完成的?

4

3 回答 3

24

对于 SimpleCursorAdapter 游标,您只需要使用 setFilterQueryProvider,根据约束为您的游标运行另一个查询:

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
  }

});

添加约束时(例如,通过使用 TextView)必须过滤适配器:

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);
  }
}

希望这可以帮助。我会尝试写一篇完整的文章,在接下来的几天里附上源代码。

于 2011-02-21T15:49:24.993 回答
7

setTextFilterEnabled()方法不会自动实现过滤,因为它不知道应该过滤文本中的什么内容。Cursor

这个android-developers 线程有更多细节。

其实前几天问了一个很好的问题,其实和你的问题很相似;尽管它最初是在询问如何在设备上没有物理键盘时处理过滤:

于 2010-01-05T04:04:10.887 回答
0

我发现这篇文章很有帮助http://androidcookbook.oreilly.com/Recipe.seam;jsessionid=CE37400B3E545937B70BE2E9F94E78BB?recipeId=404

基本上,你setTextFilterEnabled(true)在你的列表视图上,你setStringConversionColumn()setFilterQueryProvider()你的SimpleCursorAdapter.

于 2010-11-30T17:43:15.470 回答