我有RecyclerView
并且我尝试使用过滤器来搜索回收器项及其工作,但是当我使用它并输入任何东西时,结果只是列表的第一项。
这是过滤方法
private List<Data> filter(List<Data> datas, String newText) {
newText = newText.toLowerCase();
final List<Data> filteredModelList = new ArrayList<>();
for (Data data : datas) {
final String text = data.getTODO_TITLE().toLowerCase();
if (text.contains(newText)) {
filteredModelList.add(data);
}
}
return filteredModelList;
}
这是菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_search, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return true;
}
@Override
public boolean onQueryTextChange(final String searchQuery) {
final List<Data> filteredModelList = filter(dbList, searchQuery);
filteredModelList.indexOf(searchQuery);
List <Data> search =filteredModelList;
todoRecyclerView.scrollToPosition(0);
recyclerAdapter = new TodoRecyclerAdapter(search);
recyclerAdapter.notifyDataSetChanged();
return false;}});
MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return true;
}
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
});
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_search) {
return true;
}
return super.onOptionsItemSelected(item);
}}
答案 0 :(得分:0)
为什么每次搜索都要创建适配器的新实例?
recyclerAdapter = new TodoRecyclerAdapter(search);
而是一次创建并调用recyclerAdapter.notifyDataSetChanged();