-3

I have two ArrayList.

private ArrayList<Friend> friendsList = new ArrayList<Friend>();
private ArrayList<Friend> myFriendsList = new ArrayList<Friend>();

First one contains all Friend of database. Second one contains only user Friend. In my search option(SearchManager) i've a ListView contains searched friends of friendsList. When i select a Friend of ListView, i want to check if the Friend exists in myFriendsList. I used following code

friendListView
            .setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    handelListItemClick(adapter.getItem(position));
                }
            });


    private void handelListItemClick(Friend friend) {

    for(Friend fr: myFriendsList){
        Log.v("Check User Name:", fr.getName());
    }
    if (myFriendsList.contains(friend)) {works with matched friend}

But it can't check the selected Friend in myFriendsList. In LogCat it show myFriendsList information. Thanks in advance.

Update Here is my Friend class

public class Friend {

private String id, name, thumbnailUrl;

public Friend() {
}

public Friend(String name, String thumbnailUrl) {
    this.name = name;
    this.thumbnailUrl = thumbnailUrl;
}

public Friend(String name, String thumbnailUrl, String id) {
    this.name = name;
    this.thumbnailUrl = thumbnailUrl;
    this.id = id; 
}

public String getID(){
    return id;
}

public void setID(String id){
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getThumbnailUrl() {
    return thumbnailUrl;
}

public void setThumbnailUrl(String thumbnailUrl) {
    this.thumbnailUrl = thumbnailUrl;
}

}

user3384985
  • 2,975
  • 6
  • 26
  • 42

2 Answers2

0

To be able to use methods to find and search like contains your custom class MUST implement correctly the hasCode and equals methods.

Here is the link about the implementing hasCode and here about implementing equals. It is Java Best Practises website that I like very much and from where I've learned about it.

Lukasz 'Severiaan' Grela
  • 6,078
  • 7
  • 43
  • 79
0

equals() is used in most collections to determine if a collection contains a given element

hashCode() of this object is calculated and used to determine where to search for the object in the hash tables.

For more information on either of these topics visit oracle webiste or take a look here

If You could decide that two Friend objects are equal to each other if just their id's are equal.

So , In your Friend Class include

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (!(o instanceof Friend)) return false;

    Friend friend = (Friend) o;

    if (!id.equals(friend.id)) return false;

    return true;
}

@Override
public int hashCode() {
    return id.hashCode();
}

Now just do,

private void handelListItemClick(Friend friend) {
    if (myFriendsList.contains(friend)) {works with matched friend}
}

Edit

You can take a look at this tutorial to learn more about the topic

Community
  • 1
  • 1
Shubhang Malviya
  • 1,525
  • 11
  • 17