如何对 SimpleCursorAdapter 支持的 Android ListView 进行文本过滤?

发布于 2024-08-16 09:31:47 字数 184 浏览 8 评论 0原文

我有一个由 SimpleCursorAdapter 支持的 ListView。

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

问题是,我不知道如何让它与 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

给我一枪 2024-08-23 09:31:47

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

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

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.

云裳 2024-08-23 09:31:47

setTextFilterEnabled() 方法不会自动实现过滤,因为它不知道您的 Cursor 中的文本应针对哪些内容进行过滤。

android 开发者帖子提供了更多详细信息。

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

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:

み格子的夏天 2024-08-23 09:31:47

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

基本上,您在列表视图上 setTextFilterEnabled(true) ,然后使用 setStringConversionColumn()setFilterQueryProvider( ) 在您的 SimpleCursorAdapter 上。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文