Android - Filter a List by Category

1.3k Views Asked by At

I have two recyclerviews, one to select a category, and one with various items belonging to various categories. When I click on a category in my first recyclerview, I would like to see only the items in that category displayed in the second recyclerview. After some research, I have arrived at this code, but when I select a category, all items in all categories are still displayed.The filtering is not happening... I'd be grateful for some pointers. Thanks in advance!

This is in my dataProvider class:

public static Predicate<DataItem> predAnimals = new Predicate<DataItem>() {
    @Override
    public boolean apply(DataItem dItem) {
        return (dItem.getCategory() == "Animals");
    }
};

public static void displayDataItems(List<DataItem> dataItemList, final Predicate<DataItem> pred) {
    for (DataItem dItem : dataItemList) {
        if (pred.apply(dItem)){
            System.out.println(dItem.toString());
        }

    }
}

And this is in my category recyclerAdapter class:

        holder.myView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String itemName = item.getItemName();
            Intent intent = new Intent (catContext, SelectionPage.class);
            intent.putExtra(ITEM_KEY_TWO, itemName);
            catContext.startActivity(intent);

            switch (itemName){
                case "Animals":
                    SampleDataProvider.displayDataItems(dataItemList, predAnimals);
                    Toast.makeText(catContext, "Animals", Toast.LENGTH_SHORT).show();
                    break;
                case "Cartoons":
                    Toast.makeText(catContext, "Cartoons", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    });
}
1

There are 1 best solutions below

0
On BEST ANSWER

Your SampleDataProvider.displayDataItems is not changing the data of the adapter of the second RecyclerView.

Change the code to update the data of the adapter of the second recyclerview and call notifyDataSetChanged of the adapter.