如何文本过滤由 SimpleCursorAdapter 支持的 Android ListView?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2002607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to text filter an Android ListView backed by a SimpleCursorAdapter?
提问by CodeFusionMobile
I have a ListView that is backed by a SimpleCursorAdapter.
我有一个由 SimpleCursorAdapter 支持的 ListView。
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()
我希望能够像过滤联系人列表一样过滤列表,只需输入即可,然后我遇到了 textFilterEnabled()
Problem is, I couldn't see how to get it to work with a SimpleCursorAdapter.
问题是,我看不到如何让它与 SimpleCursorAdapter 一起工作。
Is this even possible?
If so, how is it done?
这甚至可能吗?
如果是这样,它是如何完成的?
采纳答案by Christopher Orr
The setTextFilterEnabled()
method doesn't automatically implement filtering, as it doesn't know whatin your Cursor
the text should be filtered against.
该setTextFilterEnabled()
方法不会自动实现过滤,因为它不知道是什么在你的Cursor
文字应该针对被过滤。
This android-developers threadhas more details.
这个android-developers 线程有更多细节。
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:
其实前几天问了一个很好的问题,其实和你的问题很相似;虽然它最初是询问当设备上没有物理键盘时如何处理过滤:
回答by radhoo
For a SimpleCursorAdapter cursor, you only need to use the setFilterQueryProvider, to run another query for your cursor, based on the constraint:
对于 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
}
});
When a constraint is added (eg. by using a TextView) the adapter must be filtered:
添加约束(例如,通过使用 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);
}
}
Hope this helps. I will try to write a complete article , with source code the next few days.
希望这可以帮助。在接下来的几天里,我将尝试写一篇完整的文章,并附上源代码。
回答by Ben H
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
.
基本上,你setTextFilterEnabled(true)
在你的列表视图,并使用setStringConversionColumn()
和setFilterQueryProvider()
你的SimpleCursorAdapter
。