我试图在我的RecyclerView中实施搜索过滤器,就像在this post中一样
我现在面临的主要问题是在我的活动中的setOnQueryTextListener:"找不到符号类OnQueryTextListenerAdapter"。这可能是我需要创建的东西,或者我可能没有在我的活动中实现某些东西。它只是MainActivity扩展AppCompatActivity。
我对活动的onCreateMenuOptions是:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
final MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(new OnQueryTextListenerAdapter() { @Override public boolean onQueryTextSubmit(String filterString) { adapter.getFilter().filter(filterString); return true; } });
return true;
}
我的适配器看起来像这样:
public class AnimeAdapter extends RecyclerView.Adapter<AnimeAdapter.AnimeViewHolder> implements INameableAdapter {
private List<Anime> items;
private List<Anime> filteredItems;
private AnimeFilter animeFilter;
用这个:
public Filter getFilter() {
if(animeFilter == null)
animeFilter = new AnimeFilter(this, items);
return animeFilter;
}
private static class AnimeFilter extends Filter {
private final AnimeAdapter adapter;
private final List<Anime> originalList;
private final List<Anime> filteredList;
private AnimeFilter(AnimeAdapter adapter, List<Anime> originalList) {
super();
this.adapter = adapter;
this.originalList = new LinkedList<>(originalList);
this.filteredList = new ArrayList<>();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
filteredList.clear();
final FilterResults results = new FilterResults();
if (constraint.length() == 0) {
filteredList.addAll(originalList);
} else {
final String filterPattern = constraint.toString().toLowerCase().trim();
for (final Anime anime : originalList) {
if (anime.getTitle().contains(filterPattern)) {
filteredList.add(anime);
}
}
}
results.values = filteredList;
results.count = filteredList.size();
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
adapter.filteredItems.clear();
adapter.filteredItems.addAll((ArrayList<Anime>) results.values);
adapter.notifyDataSetChanged();
}
我忘记了什么?
提前致谢!