SearchListAdapter.java
RecyclerView List Search using EditText implementing filterable do not work. Please check out this code. Here in my case the list is filtered but recyclerView adapter is not changed due to which list remains same.
/**
* Created by Dell on 4/8/2016.
*/
public class SearchListAdapter extends RecyclerView.Adapter<SearchListAdapter.ViewHolder> implements Filterable {
private Context context;
public static ArrayList<ChapterListModel> chapterList;
private List<ChapterListModel> orignalChapterList = new ArrayList<>();
private final List<ChapterListModel> filteredChapterList;
public SearchListAdapter(Context context, ArrayList<ChapterListModel> chapterList) {
this.context = context;
this.chapterList = chapterList;
orignalChapterList = chapterList;
filteredChapterList = new ArrayList<>();
}
@Override
public SearchListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(
LayoutInflater.from(parent.getContext()).inflate(R.layout.single_row_chapter_list, parent, false)
);
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.title.setText(chapterList.get(position).getTitle());
}
@Override
public int getItemCount() {
return chapterList.size();
}
@Override
public Filter getFilter() {
return new CustomFilter(this, orignalChapterList);
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView title;
public ViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.textView);
}
}
public class CustomFilter extends Filter {
private SearchListAdapter adapter;
private final List<ChapterListModel> originalList;
private final List<ChapterListModel> filteredList;
private CustomFilter(SearchListAdapter adapter, List<ChapterListModel> 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 ChapterListModel chapter : originalList) {
if (chapter.getTitle().contains(filterPattern)) {
filteredList.add(chapter);
}
}
}
results.values = filteredList;
results.count = filteredList.size();
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
adapter.filteredChapterList.clear();
adapter.filteredChapterList.addAll((List<ChapterListModel>) results.values);
adapter.notifyDataSetChanged();
}
}
}
Your
onBindViewHolder
andgetItemCount
methods should be using thefilteredChapterList
. Right now, you are filtering your list, correctly notifying the adapter of the change, but you're always using the original list!In your constructor, you should also change
filteredChapterList = new ArrayList<>(originalChapterList)
Finally done but using another way, not implementing fiterable.