Question

I am trying to sort a list of Facebook contacts in an android app that is using the Facebook API. First part should be contacts that are online (sorted alphabetically) and the second one the offline ones (also sorted alphabetically). So I implemented the Comparable interface and overrode compareTo() in the class for the items that need to be sorted.

public class FBRowItem implements Comparable {

private Bitmap imageId; // pazi bese int, za R.resurs
private String contactName;
private String userName;
private Boolean isAvailable;

public FBRowItem() {
    this.imageId=null;
    this.contactName=null;
    this.userName=null;
    this.isAvailable=false;
}
public FBRowItem(Bitmap imageId, String contactName, String userName, Boolean isAvailable) {
    this.imageId = imageId;
    this.contactName=contactName;
    this.userName = userName;
    this.isAvailable=isAvailable;
}

public Bitmap getImageId() {
    return imageId;
}
public void setImageId(Bitmap imageId) {
    this.imageId = imageId;
}
public String getContactName() {
    return contactName;
}
public void setContactName(String contactName) {
    this.contactName=contactName;
}
public String getUserName() {
    return userName;
}
public void setUserName(String userName) {
    this.userName=userName;
}
public Boolean getIsAvailable (){
    return isAvailable;
}
public void setIsAvailable (Boolean isAvailable){
    this.isAvailable=isAvailable;
}

@Override
public String toString() {
    return contactName;
}
@Override
public int compareTo(Object arg0) {
    FBRowItem f = (FBRowItem) arg0;

    if((this.getIsAvailable()).equals(true)){
        if((f.getIsAvailable()).equals(true))
            return this.contactName.compareTo(f.contactName);
        else return -1;
    }
    else{
        if((f.getIsAvailable()).equals(true))
            return +1;
        else
            return this.contactName.compareTo(f.contactName);
    }



    // return this.contactName.compareTo(f.contactName);
}

}

Here is the method that I call for sorting the contacts.

public static void receiveAllFBContacts(Context context){

    rowItems = FB_FriendsActivity.friendsList(context);
    Collections.sort(rowItems); 

}

As you can see in the compareTo() method, if I use the last line that is actually only a comment at the moment, the Collections.sort(rowItems) works just fine, so I have all contacts sorted, but not with the separation of online and offline. Problem: However, if I use the non-commented section as seen above, Collection.sort(rowItems) changes(!!) the statuses of all contacts to online for some reason. Tried reading some documentation, but couldn't get further. Any help is welcomed.

(rowItems is ArrayList "<"FBRowItem">" )

Was it helpful?

Solution

The code you posted can't cause the behavior you're seeing. I suggest looking elsewhere. Begin by debugging. Place a break point in your setIsAvailable method, see what's actually changing the status. (Its not your sorting.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top