我正在尝试在我的应用程序中实现一个 searchView,使用户能够跨多个列表搜索文本。如下所示:
到目前为止,我已经能够实现一个 searchView,它在单个列表中搜索文本并且效果很好,但是我想不出一种方法可以让它在显示部分标题的多个列表中工作。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
item = menu.findItem(R.id.action_search);
searchView = (SearchView) MenuItemCompat.getActionView(item);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setSuggestionsAdapter(mAdapter);
searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
@Override
public boolean onSuggestionSelect(int position) {
return false;
}
@Override
public boolean onSuggestionClick(int position) {
// some code here
return true;
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String query) {
populateAdapter(query);
return true;
}
});
// return true;
return true;
}
private void populateAdapter(String query) {
final MatrixCursor c = new MatrixCursor(new String[]{BaseColumns._ID, "cityName", "country"});
for (City city: list) {
if (city.getTitle().toLowerCase().startsWith(query.toLowerCase()))
c.addRow(new Object[]{city.getID(), city.getTitle(), city.getNation()});
}
mAdapter.changeCursor(c);
}
任何帮助将不胜感激,谢谢!