0
我使用Section Headers實現RecyclerView。我已經實施了部分標題,我的String[] names
已分別被過濾到他們的部分。我無法過濾剩餘的String []存在,可用&圖像。我怎樣才能將其他字符串排序到它們各自的部分?使用RecyclerView將模型按字母順序排序
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;
}
你能再次請帖嗎?我添加了代碼,但它不會將其餘對象過濾到其各自的區域 – Contextioner
您可以將compareTo值組名更改爲Presence或Available或Image: return object1.getPresence()。compareTo(object2。 getPresence()); return object1.getAvailable()。compareTo(object2.getAvailable()); return object1.getImage()。compareTo(object2.getImage()); 你想要的方式。 –
您可以發佈完整的代碼,我應該如何完成這項工作?因爲它還沒有爲我工作 – Contextioner