0

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

Community
  • 1
  • 1
remy boys
  • 2,928
  • 5
  • 36
  • 65
  • 1
    Have a look at this answer : (http://stackoverflow.com/questions/30752980/add-two-sections-in-recyclerview-android) – Ashlesha Sharma Mar 09 '16 at 07:03
  • @AshleshaSharma hey man thanks a lot for responding , by seeing your link i guess my question is not clear enough , let me update it , and i already added the section i was talking about adding values to section , thanks a lot :) appreciate your effort – remy boys Mar 09 '16 at 07:07
  • @AshleshaSharma please check my updated question – remy boys Mar 09 '16 at 07:25

1 Answers1

1

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.

Ashlesha Sharma
  • 949
  • 7
  • 15