How To Remove Duplicate Contact From Contact List In Android
Solution 1:
You should modify your ContactsEntityBean
like below
publicclassContactsEntityBean {
privateHashSet<String> emails = newHashSet<String>();
publicvoidsetEmail(String email) {
if (email == null)
return;
this.emails.add(email.trim());
}
publicHashSet<String> getEmails() {
returnthis.emails;
}
}
Will care about duplicate emails... you can use same logic for addresses, phones etc.
Replace your ContactsEntityBean
with below code
publicclassContactsEntityBean {
privateHashSet<String> emails;
privateHashSet<String> phones;
privateHashSet<String> addresses;
privateString contactId;
privateboolean checked = false;
publicContactsEntityBean() {
this.emails = newHashSet<String>();
this.phones = newHashSet<String>();
this.addresses = newHashSet<String>();
}
publicHashSet<String> getPhones() {
return phones;
}
publicvoidsetPhones(String phone) {
if (phone == null)
return;
this.phones.add(phone.trim());
}
publicHashSet<String> getAddresses() {
return addresses;
}
publicvoidsetAddresses(String address) {
if (address == null)
return;
this.addresses.add(address.trim());
}
publicvoidsetEmails(String email) {
if (email == null)
return;
this.emails.add(email.trim());
}
publicHashSet<String> getEmails() {
return emails;
}
publicStringgetContactId() {
return contactId;
}
publicvoidsetContactId(String contactId) {
this.contactId = contactId;
}
publicbooleanisChecked() {
return checked;
}
publicvoidsetChecked(boolean checked) {
this.checked = checked;
}
}
And no need to care about duplicates. this will care about all the things..
Solution 2:
This is worked form me
privatevoidgetContactDetails(ContentResolver contentResolver) {
Cursor phones = contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
null, null);
HashSet<String> mobileNoSet = newHashSet<String>();
while (phones.moveToNext()) {
String name = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String email = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
String imagUri = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Photo.PHOTO_URI));
long id = phones.getColumnIndex(ContactsContract.Contacts._ID);
if (!mobileNoSet.contains(phoneNumber)) {
arrayContacts.add(newContact(name, phoneNumber, email,
imagUri, id));
mobileNoSet.add(phoneNumber);
}
}
adapterContact = newAdapterContact(getActivity(), arrayContacts);
listContact.setAdapter(adapterContact);
}
Solution 3:
Save your contacts in a ArrayList and then remove duplicates from the list. The easiest way to remove repeated elements is to add the contents to a Set (which will not allow duplicates) and then add the Set back to the ArrayList.
Use HashSet
like this.
ArrayListal=newArrayList();
// add elements to al, including duplicatesHashSeths=newHashSet();
hs.addAll(al);
al.clear();
al.addAll(hs);
Solution 4:
By changing you method like this your can get the only one contact which is not duplicate..
publicstaticArrayList<ContactsEntityBean> getContactDetails(Context mContext) {
ArrayList<ContactsEntityBean> contactList = newArrayList<ContactsEntityBean>();
ContentResolver cr = mContext.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
HashMap<String, String> data = newHashMap<String, String>();
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
Cursor cur1 = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = ?", newString[] { id }, null);
while (cur1.moveToNext()) {
ContactsEntityBean contactsEntityBean = newContactsEntityBean();
// to get the contact namesString name = cur1
.getString(cur1
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
// Log.e("Name :", name);String email = cur1
.getString(cur1
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
if (!data.containsValue(email)) {
// Log.e("Email", email);
contactsEntityBean.setName(name);
contactsEntityBean.setEmail(email);
data.put(name, email);
if (email != null) {
contactList.add(contactsEntityBean);
}
}
}
cur1.close();
}
}
return contactList;
}
Solution 5:
Use HashMap because HashMap does not allow duplicate keys
Declare A Global Variable
// Hash MapsMap<String, String> nameEmailMap = newHashMap<String, String>();
In Your Loop Add Data into HashMap
// Enter Into Hash Map
nameEmailMap.put(email, name);
Outside the Loop Get The Log
// Get The Contents of Hash Map in Logfor (Map.Entry<String, String> entry : namePhoneMap.entrySet()) {
String key = entry.getKey();
Log.d(TAG, "Phone :" + key);
String value = entry.getValue();
Log.d(TAG, "Name :" + value);
}
For my full answer on how to retrieve contacts without duplicates please see https://stackoverflow.com/a/54227282/3904109 (For Emails) and https://stackoverflow.com/a/54228199/3904109 (For Phones)
Post a Comment for "How To Remove Duplicate Contact From Contact List In Android"