I have been working on an android app to show contacts
in the list. I am using recyclerview
and i am able to show the contacts with name and image in the list.
But my list contains the duplicate of contact numbers. I used the below code to remove the duplicates but it shows
9566191161
+919566191161
as two entries. (ie.,) Along with country code, it is showed as a separate entry.
I am using a POJO class and adding it to Adapter as a List. In Pojo Class, i used the coding to remove duplicates like
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stubs
if (obj instanceof ContactVO) {
ContactVO temp = (ContactVO) obj;
if (this.getContactNumber() == temp.getContactNumber() && this.getContactName() == temp.getContactName() && (this.getContactNumber()).contains(temp.getContactNumber()))
return false;
}
return true;
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return (this.getContactNumber().hashCode() + this.getContactName().hashCode());
}
}
I used as "+91"+this.getContactNumber().contains(temp.getContactNumber())
validation in the equals method but it doesn't remove that duplicate.
Can you guys please help me in this error.
My code snippet
Cursor phones = getContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
while (phones.moveToNext()) {
name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)).replace(" ", "");
imageUri = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
System.out.println("Name and Phone number = " + name + phoneNumber + imageUri);
contactVOList.add(new ContactVO(imageUri, name, phoneNumber));
System.out.println("List size before removing duplicates =" + contactVOList.size());
}
Set<ContactVO> s = new HashSet<ContactVO>();
s.addAll(contactVOList);
contactVOList = new ArrayList<ContactVO>();
contactVOList.addAll(s);
// contactVOList = removeDuplicates(contactVOList);
System.out.println("List size after removing duplicates =" + contactVOList.size());
System.out.println("ListSize before sendinf" + contactVOList.size());
mAdapter = new AllContactsAdapter(getContext(), contactVOList, userId, mobilenumber);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getContext());
rvContacts.setLayoutManager(mLayoutManager);
rvContacts.setItemAnimator(new DefaultItemAnimator());
rvContacts.setAdapter(mAdapter);
Happy new year guys !