0

I have a RecyclerView which will show a list of items. I want to provide a filter for my list so that if user select A, the adapter will skip other types and only show A.

public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder> {
    private List<Item> mItems;
    private String mSelectedType;

    @Override
    public void onBindViewHolder(ItemViewHolder holder, int position) {
        Item item = mItems.get(position);
        if (mSelectedType.equals(item.getType())) {
            //bind it
        } else {
            //skip it
        }
    }

    public void setSelectedType(String selectedType) {
        mSelectedType = selectedType;
    }
}
Sam
  • 351
  • 1
  • 2
  • 15
  • 1
    instead why don't you refresh your mItems list to not contain deleted items or if you still need to have them, create a tempList without deleted items and put that to adapter. – Kapil G Aug 11 '17 at 13:47
  • If it is already deleted then it should not display in list.How it is getting deleted ? – FaisalAhmed Aug 11 '17 at 13:47
  • Hello, check out my answer [here](https://stackoverflow.com/a/54272566/5670752) – Taslim Oseni Jan 20 '19 at 00:33

3 Answers3

1

Using For the:~

remove(position);
notifyItemRemoved(position);
Harshit Trivedi
  • 764
  • 9
  • 33
1

If you're passing global List<Item> mItems to Adapter constructor and using this list to populate RecycleView, then in the same place where you remove the item from the list, you should just call adapter.notifyDataSetChanged(), and that item will no longer appear in the list.

Gotiasits
  • 1,135
  • 1
  • 10
  • 21
0

you have to remove isDeleted() item from your list which you bind in your RecyclerView. then after you have to bind in RecyclerView.

Mehul Kabaria
  • 6,404
  • 4
  • 25
  • 50