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

时间:2010-01-04 22:06:26

标签: database android listview filter cursor

我有一个由SimpleCursorAdapter支持的ListView。

我希望能够按照您的联系人列表过滤列表,只需键入,我就遇到了textFilterEnabled()

问题是,我无法看到如何使用SimpleCursorAdapter。

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

3 个答案:

答案 0 :(得分: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);
  }
}

希望这会有所帮助。我将尝试撰写一篇完整的文章,其中包含未来几天的源代码。

答案 1 :(得分:7)

setTextFilterEnabled()方法不会自动实施过滤,因为它不知道Cursor中应该过滤的文本中的

android-developers thread有更多详情。

实际上,前几天有一个很好的问题,实际上与你的问题非常相似;虽然它最初是在设备上没有物理键盘时询问如何处理过滤:

答案 2 :(得分:0)

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

基本上,您在列表视图中setTextFilterEnabled(true),并在setStringConversionColumn()上使用setFilterQueryProvider()SimpleCursorAdapter