Reputation: 75
I implementing RecyclerView with Section Headers. I have implemented the Sections Headers and my String[] names
have been filtered into their sections respectively. I am unable to filters the rest of the String[] presence, available & images. How can I sort the other Strings into their respective sections?
public GroupChatFragment(){}
String[] names;
String[] presence;
String[] available;
String[] images;
@Override
protected void initView(LayoutInflater inflater) {
super.initView(inflater);
Resources resources = getResources();
names = resources.getStringArray(R.array.group_names);
presence = resources.getStringArray(R.array.users_presence);
available = resources.getStringArray(R.array.users_available);
images = resources.getStringArray(R.array.group_images);
}
@Override
protected void addDataToAdapter(SectionedRecyclerAdapter adapter) {
for (char alphabet = 'A'; alphabet <= 'Z'; alphabet++) {
ArrayList<Group> groups = getHeadersWithLetter(alphabet);
if (groups.size() > 0) {
adapter.addSection(new GroupChatSection(String.valueOf(alphabet), groups));
}
}
}
private ArrayList<Group> getHeadersWithLetter(char letter) {
ArrayList<Group> groupArrayList = new ArrayList<>();
for (String contact : names) {
if (contact.charAt(0) == letter) {
Group group = new Group();
group.setGroupName(contact);
for (int i = 0; i < 18; i++){
group.setAvailableUsers(available[i]);
group.setGroupImage(images[i]);
group.setNumUsers(presence[i]);
}
groupArrayList.add(group);
}
Collections.sort(groupArrayList, new Comparator<Group>() {
@Override
public int compare(final Group object1, final Group object2) {
return object1.getGroupName().compareTo(object2.getGroupName());
}
});
}
return groupArrayList;
}
Upvotes: 0
Views: 1572
Reputation: 521
you have to use collection sort with Comparator for sorting model class, check this:
Collections.sort(groupArrayList, new Comparator<Group>() {
@Override
public int compare(final Group object1, final Group object2) {
String obj1 =object1.getGroupName()+object1.getGroupImage() + object1.getAvailableUsers() +object1.getNumUsers();
String obj2 =object2.getGroupName()+object2.getGroupImage() + object2.getAvailableUsers() +object2.getNumUsers();
return obj1.compareTo(obj2);
}
});
Add this lines above return statement in getHeadersWithLetter method, hope this will help you..:)
Upvotes: 1