Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to write an algorithm for filtering out items in a RecyclerView based on a long saved with each item

I have several items in a RecyclerView and each item has a long value saved with it. I'm using FastAdapter as the adapter for my RecyclerView.

Suppose there are 7 items in the RecyclerView with the long values: 11122, 12321, -98811, 8870, -88009, 3398, and -22113.

So, what I want to do is, I want to filter the items based on the above given long values using this logic:

if (l <= 1000) {
  // show items with long value <=1000
} else if (l > 1000) {
  // show items with long value >1000
}

I tried various things, but nothing worked out.

UPDATE 1: Items here are a sort of different data stored in CardView and then shown in RecyclerView. Each card contains different data, one of which are the above given long values. I want to filter the data based on these long values stored in each card based on the logic given above.

Please help me with this issue and suggest some algorithm or code with which I can achieve this.

like image 848
Hammad Nasir Avatar asked Dec 30 '16 02:12

Hammad Nasir


2 Answers

With the amount of information given I can only suppose l is a foreign selector value which controls the items to be displayed inside the RecyclerView. Comment below if this is not the case, I will try to correct my answer.

I recommend implementing a custom ViewAdapter, sending in the list of items and the selector variable l using respective methods:

public class ItemsAdapter extends 
    RecyclerView.Adapter<ItemsAdapter.ItemViewHolder> {

    private List<Long> mItemList;
    private List<Long> mDisplayItems;
    private boolean mAboveThousand = true;

    public void setItemList(List<Long> list) {
        mItemList = list;
        updateDisplayItems();
    }

    public void setSelectionType(boolean aboveThousand) {
        mAboveThousand = aboveThousand;
        updateDisplayItems();
    }

    private updateDisplayItems() {
        mDisplayItems.clear();

        for(Long item: mItemList) {
            if(/*check your contition*/) {
                mDisplayItems.add(item);
            }
        }

        notifyDataSetChanged(); //important
    }

    ...
    // Rest of implementation
}

Also, I have never used FastAdapter, but I suppose there must be some methods to override if you extend its class.

Update

Since, you are facing problems understanding the basics of using a ViewAdapter, I would recommend learning and implementing a custom ViewAdapter before using any library. Here's a extensive tutorial for how to implement ViewAdapter for RecyclerView.

Now, after you have implemented the ViewAdapter you can use my piece of code to filter out cards. Basically, what the code is doing is saving a list of all the required data inside mItemList, while mDisplayList is a list storing the items to be displayed, which is updated every-time mAboveThousand, which stores the user preference of above or below 1000, is set. Now this mDisplayList must be used to inflate data inside the RecyclerView.

like image 98
Abhishek Avatar answered Oct 11 '22 04:10

Abhishek


Even your very basic code there would work. You can count the number of items in that range and return the number in that range. I suggest you try to do this without FastAdapter because the core concept of parsing the data based on a filter value is rightly perfectly solid. You can iterate the loop and count them, and you can iterate the loop and return the nth item.

like image 4
Tatarize Avatar answered Oct 11 '22 02:10

Tatarize