当我的 EditText 中发生 OnTextChange 时,我尝试通过修改光标的选择来过滤联系人列表。问题:列表保持原样,即它不会根据过滤器进行更新。我究竟做错了什么?我怀疑这是我的 sql 查询(特别是 GLOB 部分)?
digitsText.addTextChangedListener(new TextWatcher(){
public void onTextChanged(CharSequence s, int start, int before, int count){
filterText = digitsText.getText().toString();
WHERE_CONDITION = ContactsContract.Data.DATA1 + " GLOB '*" + filterText + "*'";
cursor = getContentResolver().query(URI, PROJECTION, WHERE_CONDITION, null, SORT_ORDER);
startManagingCursor(cursor);
setListAdapter(adapter);
}
});
最佳答案
当你生成一个新的Cursor
时,你实际上并没有用它做任何事情。您需要:
- 如果您使用的是 API 级别 11 或更高版本,请在
CursorAdapter
上调用swapCursor()
- 在
CursorAdapter
上调用changeCursor()
(如果您不需要旧的Cursor
,这可能是更好的选择,因为changeCursor()
将为您关闭它) - 创建一个新的
CursorAdapter
并使用新适配器调用setListAdapter()
关于java - 使用 WHERE 条件过滤游标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7805521/