我试图过滤一个listview,它通过SimpleCursorAdapter连接到数据库。我在这个主题上阅读了很多其他问题,但我无法让它发挥作用。当我输入一个单词时,没有任何事情发生。我也很高兴按钮点击过滤列表过滤。 有谁知道问题是什么?
相关方法:
private void initListView() {
listView = (ListView) findViewById(R.id.list);
}
private void populateListView(){
Cursor cursor = myDb.getAllItems();
String[] fromFieldNames = new String[] {Database.COL_IMAGE, Database.COL_DATE, Database.COL_HEAD};
int[] toViewIds = new int[] {R.id.list_view_image, R.id.list_view_date, R.id.list_view_headline};
cursorAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.list_view_search, cursor, fromFieldNames, toViewIds, 0);
listView.setAdapter(cursorAdapter);
listView.setTextFilterEnabled(true);
}
private void initSearch() {
cursorAdapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
String catchword = null;
if(constraint!=null){
catchword = constraint.toString();
}
return myDb.getWordMatches(catchword);
}
});
searchText = (EditText) findViewById(R.id.search_edit_text);
searchText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
public void afterTextChanged(Editable editable) {
SimpleCursorAdapter filterAdapter = (SimpleCursorAdapter) listView.getAdapter();
filterAdapter.getFilter().filter(editable.toString());
}
});
}
这是我数据库中的相关方法:
public Cursor getWordMatches(String query) {
return database.query(TABLE_NAME, new String[] {COL_DATE, COL_HEAD, COL_IMAGE, COL_MAP, COL_TEXT}, "COL_HEAD like ?", null, null, null, "%'"+ query +"'%");
}
谢谢!