0

I am trying to populate a RecyclerView with a custom adapter. This Adapter takes in a model that has setters and getters. This model contains three parameters: String username, int proPic, and int bckgImg. What i am trying to achieve is a RecyclerView with an alphabetical sorted list. I populate the model by using the following code:

private ArrayList<FriendsModel> friendsData = new ArrayList<>(0);
friendsData.add(new FriendModel("Erick", R.drawable.default_pro_pic, R.drawable bckgImg);

After i am done with populating 5 objects of the FriendModel, i then proceed to compare the characters found in the 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;
}

I am not sure if i am doing this correctly, what is the adequate or correct way to compare the characters in an ArrayList based on its model?

Erick Ramirez
  • 157
  • 1
  • 3
  • 9
  • Char is convert to byte and compare – Raj Jun 13 '17 at 09:09
  • You can check this Q&A: https://stackoverflow.com/questions/708698/how-can-i-sort-a-list-alphabetically – Jure Jun 13 '17 at 09:11
  • Hi, thank you for pointing me in the right direction. What if the ArrayList is not a String ArrayList but a custom ArrayList as defined by a model above? I want to be able to extract the first parameter of each sequential object found in friendsData and then compare it to the alphabet letters A-Z at position 0 for the character in that given object. – Erick Ramirez Jun 13 '17 at 09:17
  • Use collection and comparator to do that job. – Sagar Pujari Jun 13 '17 at 09:58

3 Answers3

1

Instead of using constructor to initialize the variables, always use the setter methods, such classes are also known as pojo(Plain old java objects) or VO(value objects).

Assuming your FriendModel class is like below..

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;
    }

}

And where ever you are doing the following,

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;
}
Darshan Miskin
  • 844
  • 14
  • 25
  • Thank you! it now works! However, is this a good way of accomplishing this? Many have stated to use the comparator with collection. Will this achieve the same thing? – Erick Ramirez Jun 13 '17 at 17:47
  • Can you explain how a comparator with a collection differ? or can you provide a small example using collection and comparator for the same use as above? This might be really beneficial for people looking to sort lists. I understand its another question so its ok if you cant. – Erick Ramirez Jun 13 '17 at 17:53
  • Honestly, comparator is new to me too, had to look it up, from my research it is a nice way for sorting custom Lists. I am glad you mentioned it! What i would suggest is having get & set methods along with constructors which initialize the class variables along with a few custom classes which compare the objects. Here is an example which uses Collections to sort Custom Lists using Comparator -> https://stackoverflow.com/a/2839167/6172439 – Darshan Miskin Jun 14 '17 at 05:44
  • Thank you kind Sir! – Erick Ramirez Jun 15 '17 at 04:08
  • The above method will only produce name list sorted by first character of name so its not a proper solution. Check my answer if you want true sorted list. – nitinkumarp Jun 17 '17 at 07:20
0

Custom Comparator should help

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

or if it's not work so us it

Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
Soni_ji
  • 115
  • 1
  • 12
0

The proper way to sort the list with model is to define Comparator. For your current example you used use.

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
  }
});

"compare" function compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

nitinkumarp
  • 2,120
  • 1
  • 21
  • 30