I have created a custom ContactsRow class
containing three variables. I am adding objects of this class to ArrayList
one-by-one. What I want to achieve is that, whenever I add new object to ArrayList
, it should check if that object is already added to the list. I am using contains()
method to check the same. However, when I run my app, this method is never called and I am getting duplicate values in the list.
Here is my ContactsRow class
:
private class ContactsRow {
String name, phone;
Bitmap photo;
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if ((obj == null) || (obj.getClass() != this.getClass()))
return false;
ContactsRow row = (ContactsRow) obj;
return (name.equals(row.name) || (name != null && name.equals(row.name))) &&
(phone.equals(row.phone) || (phone != null && phone.equals(row.phone))) &&
(photo.equals(row.photo) || (photo != null && photo.equals(row.photo)));
}
@Override
public int hashCode() {
int hash = 7;
hash = 31 * hash + (name == null ? 0 : name.hashCode());
hash = 31 * hash + (phone == null ? 0 : phone.hashCode());
hash = 31 * hash + (photo == null ? 0 : photo.hashCode());
return hash;
}
public ContactsRow(String name, String phone, Bitmap photo) {
this.name = name;
this.phone = phone;
this.photo = photo;
}
}
Here is how I am adding and checking values in ArrayList
:
contactsRow = new ContactsRow(contactName, contactNumber, contact_Photo);
if(contactsRowList.contains(contactsRow)){
AlertDialog.Builder duplicateBuilder = new AlertDialog.Builder(MyContactsActivity.this);
duplicateBuilder.setTitle("Duplicate contact");
duplicateBuilder.setMessage("This contact is already added to list. Please choose unique contact");
duplicateBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(intent, REQUEST_CONTACT);
duplicateEntryDialog.dismiss();
}
});
duplicateEntryDialog = duplicateBuilder.create();
duplicateEntryDialog.show();
} else {
contactsRowList.add(contactsRow);
contactsInfoAdapter.notifyDataSetChanged();
}
This AlertDialog
is never called.
Can anyone help me identify the issue????