remy boys
remy boys

Reputation: 2948

add values into section in recyclerView

i followed this question's answer for adding sections in my recyclerView

for adding the sections we have to provide the index for section like this :

//Sections
sections.add(new SimpleSectionedRecyclerViewAdapter.Section(0,"Section 1"));
sections.add(new SimpleSectionedRecyclerViewAdapter.Section(5,"Section 2"));

so the section 1 will be at index 0 and section 2 will be at index 5

now i have dataset of ChannelsInformation and channelInformation class is :

public class ChannelsInformation {
String id,channelName;
boolean isSelected , isfavorite;
}

so my question is that what i have to do for adding my dataSet into different sections ? suppose i have a data

<id = 1 , channelName = channelName1 , isSelcted = false , isfavorite = true >
2 , channelName2 , false , false 
3 , channelName3 , false , true 
4 , channelName4 , false , false 

so the above is a demo of some data now i want to add that data into my recyclerView according two isFavorite field , after adding the data in recylerView it should look like this :

section 1
1 - channelName1
3 - channelName3
section 2
2 - channelName2
4 - channelName4

any idea what i have to do for adding my data in a form so that it'll organise as the above example ?

this is how am populating the data into my recyclerView (on my fragment class) :

    public static List<ChannelsInformation> getData(){

    List<ChannelsInformation> data = new ArrayList<>();

    for (int i = 0 ; i<channelsArray.size() && i<channelsIDArray.size() ; i++ ){

        ChannelsInformation current  = new ChannelsInformation();
        current.channelName = channelsArray.get(i);
        current.id = channelsIDArray.get(i);
        current.isFavorite = channelIsFavoriteArray.get(i);

        data.add(current);
    }

    return  data;

}

any idea how am gonna do that ? any hint or guidance will be highly appreciated

if code is not sufficient then please let me know i'll update it

Upvotes: 0

Views: 104

Answers (1)

Ashlesha Sharma
Ashlesha Sharma

Reputation: 949

you can sort your list on the basis of boolean "isfavorite" as follows:

public class ChannelsInformation implements Comparable<ChannelsInformation> {
    String id, channelName;
    boolean isSelected, isfavorite;



    @Override
    public int compareTo(ChannelsInformation o) {
        int b1 = this.isfavorite ? 1 : 0;
        int b2 = o.isfavorite ? 1 : 0;

        return b2 - b1;
    }

}

And then call Collections.sort(data); after setting your arraylist.

Upvotes: 1

Related Questions