2017-06-13 64 views
0

我想用自定義適配器填充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中的字符?

+0

char轉換爲字節並比較 – Raj

+0

您可以查看本問與答:https://stackoverflow.com/questions/708698/how-can-i-sort-a-list-alphabetically – Jure

+0

嗨,謝謝你指點我在正確的方向。如果ArrayList不是字符串ArrayList,而是由上述模型定義的自定義ArrayList?我希望能夠提取friendsData中找到的每個連續對象的第一個參數,然後將其與位於給定對象中字符位置爲0的字母A-Z進行比較。 –

回答

1

而不是使用構造函數初始化變量,始終使用setter方法的,這樣的類也是已知的作爲pojo(普通的舊Java對象)或VO(值對象)。

假設你FriendModel類就像下面..

public class FriendModel{ 
    String username; 
    int proPic; 
    int bckgImg; 

    public FriendModel(){} 

    //redundant constructor, you can remove this since you are using setter methods now. 
    public FriendModel(String username, int proPic, int bckgImg){ 
     this.username=username; 
     this.proPic=proPic; 
     this.bckgImg=bckgImg; 
    } 

    //you'll need the below getters and setters.. 
    public String getUsername() { 
     return username; 
    } 

    public void setUsername(String username) { 
     this.username = username; 
    } 

    public int getProPic() { 
     return proPic; 
    } 

    public void setProPic(int proPic) { 
     this.proPic = proPic; 
    } 

    public int getBckgImg() { 
     return bckgImg; 
    } 

    public void setBckgImg(int bckgImg) { 
     this.bckgImg = bckgImg; 
    } 

} 

和日益你正在做以下命令,其中,

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 
String username=friendsData.get(i).getUsername();  //get the username 
if(username.charAt(0) == alph){      //compare the username with charater in alph variable 
friends.add(username);         //add in another list 
} 
} 

return friends; 
} 
+0

謝謝!它現在工作!但是,這是完成這個的好方法嗎?許多人表示要使用收集器進行比較。這會實現相同的目的嗎? –

+0

你能解釋一個集合的比較器是如何不同的?或者你能否提供一個使用收集和比較器的小例子來達到與上面相同的目的?這對於希望對列表進行排序的人來說可能非常有用。我瞭解它的另一個問題,所以它可以,如果你不能。 –

+0

老實說,比較器對我來說也是新的,必須從我的研究中查找它,這是排序自定義列表的好方法。我很高興你提到它!我所建議的是讓get&set方法和構造函數一起初始化類變量以及一些比較對象的自定義類。這是一個使用集合來使用比較器對自定義列表進行排序的示例 - > https://stackoverflow.com/a/2839167/6172439 –

0

自定義比較應該有助於

Collections.sort(list, new Comparator<String>() { 
    @Override 
    public int compare(String s1, String s2) { 
     return s1.compareToIgnoreCase(s2); 
    } 
}); 

,或者如果它沒有這樣的工作我們來說

Collections.sort(list, String.CASE_INSENSITIVE_ORDER); 
+0

如果你可以解釋一下它是如何工作的,以及它爲什麼起作用,那就太好了,並不是每個人都熟悉比較器。 –

0

排序與模型列表中的正確方法是定義比較。對於您當前使用的示例。

Collections.sort(friendsData, new Comparator<FriendsModel>() { 
@Override 
public int compare(FriendsModel obj1, FriendsModel obj2) { 
    return obj1.getUsername.compareTo(obj2.getUsername); //For Ascending order 
//OR 
    return obj2.getUsername.compareTo(obj1.getUsername); //For Descending order 
    } 
}); 

「比較」功能,它的兩個參數的順序進行比較。由於第一個參數小於,等於或大於第二個參數,因此返回負整數,零或正整數。