2010-01-04 51 views

回答

7

setTextFilterEnabled()方法不会自动实现过滤,因为它不知道在您的Cursor中应该过滤哪些文字

android-developers thread有更多的细节。

事实上,前几天有一个很好的问题,其实和你的问题非常相似,虽然它最初是问如何处理过滤的时候没有物理键盘的设备上:在您的列表视图

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

如果您包含选择示例,那么很好的答案是少数使用游标显示runQuery部件的答案 - 会更好。例如。 selection = FIELD_NAME_TO_FILTER +“like”“+ constraint.toString()+”'“; – DEzra 2011-11-22 11:15:25

+2

如果我们使用SQLite数据库,我们应该如何在runQuery中调用游标?使用与用于为SimpleCursorAdapter创建游标的实例相同的databaseHelper实例给我一个“fillWindow()中的无效语句”错误。 – rohitmishra 2012-02-24 13:43:43

+1

m_Adapter是否存在m_slvAdapter状态? – r4m 2013-11-13 08:49:39

相关问题