我在数据库中有一个国家列表。 我创建了一个选择国家的活动,包括一个用于过滤的编辑框和一个显示旗帜和国家名称的列表。
当活动开始时,列表按字母顺序显示整个国家列表 - 运行良好。 当用户开始在搜索框中输入时,我希望根据他们的输入筛选列表。 我之前在AutoCompleteView中使用过我的数据库查询(我只想切换到单独的文本框和列表),所以我知道我的完整查询和约束查询是可用的。 我所做的是在EditText视图中添加TextWatcher,并且每次更改文本时,我使用编辑框的文本作为约束调用列表的SimpleCursorAdapter runQueryOnBackgroundThread。 问题是列表从未被更新。 我已在调试器中设置断点,TextWatcher确实调用runQueryOnBackgroundThread,并且我的FilterQueryProvider将使用预期的约束进行调用。 数据库查询正常进行,返回光标。
游标适配器设置了过滤器查询提供程序(和视图绑定器以显示旗帜):
SimpleCursorAdapter adapter = new SimpleCursorAdapter (this,
R.layout.country_list_row, countryCursor, from, to);
adapter.setFilterQueryProvider (new CountryFilterProvider ());
adapter.setViewBinder (new FlagViewBinder ());
过滤查询提供程序(FitlerQueryProvider):
private final class CountryFilterProvider implements FilterQueryProvider {
@Override
public Cursor runQuery (CharSequence constraint) {
Cursor countryCursor = myDbHelper.getCountryList (constraint);
startManagingCursor (countryCursor);
return countryCursor;
}
}
EditText有一个TextWatcher:
myCountrySearchText = (EditText)findViewById (R.id.entry);
myCountrySearchText.setHint (R.string.country_hint);
myCountrySearchText.addTextChangedListener (new TextWatcher() {
@Override
public void afterTextChanged (Editable s) {
SimpleCursorAdapter filterAdapter = (SimpleCursorAdapter)myCountryList.getAdapter ();
filterAdapter.runQueryOnBackgroundThread (s.toString ());
}
@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
// no work to do
}
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {
// no work to do
}
});
数据库的查询语句如下:
public Cursor getCountryList (CharSequence constraint) {
if (constraint == null || constraint.length () == 0) {
// Return the full list of countries
return myDataBase.query (DATABASE_COUNTRY_TABLE,
new String[] { KEY_ROWID, KEY_COUNTRYNAME, KEY_COUNTRYCODE }, null, null, null,
null, KEY_COUNTRYNAME);
} else {
// Return a list of countries who's name contains the passed in constraint
return myDataBase.query (DATABASE_COUNTRY_TABLE,
new String[] { KEY_ROWID, KEY_COUNTRYNAME, KEY_COUNTRYCODE },
"Country like '%" + constraint.toString () + "%'", null, null, null,
"CASE WHEN Country like '" + constraint.toString () +
"%' THEN 0 ELSE 1 END, Country");
}
}
似乎有一些遗漏的地方,需要找到这个遗漏的环节。非常感谢您的帮助。
谢谢,
Ian