我想用自定義適配器填充RecyclerView。這個適配器接受一個有setter和getters的模型。該模型包含三個參數:字符串用戶名,int proPic和int bckgImg。我試圖實現的是一個帶有按字母排序的列表的RecyclerView。我填充使用下面的代碼模型:比較ArrayLists中的字符?
private ArrayList<FriendsModel> friendsData = new ArrayList<>(0);
friendsData.add(new FriendModel("Erick", R.drawable.default_pro_pic, R.drawable bckgImg);
我與填充FriendModel的5個對象完成後,我然後進行比較ArrayList中發現的字符:
for(char alph = 'A'; alph <= 'Z'; alph++){
List<String> friends = getFriends(alph);
if(friends.size() > 0){
//Populate the Sorted Alphabetical view
}
}
private List<String> getFriends(char alph) {
//Empty List used to populate when comparing existing model to alphabet
List<String> friends = new ArrayList<>();
//5 objects of FriendsData with three parameters
for (int i = 0; i < friendsData.size(); i++){
//If the first string parameter character at position 0 is equal to alph
if(friendsData.get(i).toString().charAt(0) == alph){
friends.add(friendsData.get(i).toString());
}
}
return friends;
}
我不知道如果我正確地做到這一點,什麼是適當的或正確的方式來比較基於其模型的ArrayList中的字符?
char轉換爲字節並比較 – Raj
您可以查看本問與答:https://stackoverflow.com/questions/708698/how-can-i-sort-a-list-alphabetically – Jure
嗨,謝謝你指點我在正確的方向。如果ArrayList不是字符串ArrayList,而是由上述模型定義的自定義ArrayList?我希望能夠提取friendsData中找到的每個連續對象的第一個參數,然後將其與位於給定對象中字符位置爲0的字母A-Z進行比較。 –