Sorted duplicate contact list, why?

I sorted and listed the contacts of my phone in arraylist, but I got a lot of duplicates of the same contact names in the list. How does this happen? how to avoid this?

This is what I tried

cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, "(" + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") ASC"); while (cursor.moveToNext()) { try { name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); phonenumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); contact_names_list.add(name); phone_num_list.add(phonenumber); } catch (Exception e) { e.printStackTrace(); } 

can anyone help?

+5
source share
7 answers

No one seems to be answering your question.

The reason you see duplicate contacts is because you are asking for phone numbers not contacts .

There are 3 main tables in Android:

  • Contacts table - has one element for each contact table
  • RawContacts - has one element for each account (for example, Google, Outlook, Whatsapp, etc.) - several RawContacts are associated with one Contact Table
  • Data - has one element for each part (name, email address, phone, address, etc.) - each data element is associated with one RawContact , and several rows of Data are associated with each RawContact .

You request CommonDataKinds.Phone.CONTENT_URI , which is part of the Data table, so if a contact has more than one phone and / or it has the same phone from several sources (for example, Google and Whatsapp), you are the same phone with the same CONTACT_ID more than once.

The solution is to use a HashMap (and not a HashSet ), where the key is CONTACT_ID , so you can map several phones to a contact:

 String[] projection = new String[] { CommonDataKinds.Phone.CONTACT_ID, CommonDataKinds.Phone.DISPLAY_NAME, CommonDataKinds.Phone.NUMBER }; Cursor cursor = getContentResolver().query(CommonDataKinds.Phone.CONTENT_URI, projection, null, null, null); HashMap<Long, Contact> contacts = new HashMap<>(); while (cursor.moveToNext()) { long id = cursor.getLong(0); String name = cursor.getString(1); String phone = cursor.getString(2); Contact c = contacts.get(id); if (c == null) { // newly found contact, add to Map c = new Contact(); c.name = name; contacts.put(id, c); } // add phone to contact class c.phones.add(phone); } cursor.close(); // simple class to store multiple phones per contact private class Contact { public String name; // use can use a HashSet here to avoid duplicate phones per contact public List<String> phones = new ArrayList<>(); } 

If you want to sort your HashMap by name:

 List<Contact> values = new ArrayList<>(contacts.values()); Collections.sort(values, new Comparator<Contact> { public int compare(Contact a, Contact b) { return a.name.compareTo(b.name); } }); // iterate the sorted list, per contact: for (Contact contact : values) { Log.i(TAG, "contact " + contact.name + ": "); // iterate the list of phones within each contact: for (String phone : contact.phones) { Log.i(TAG, "\t phone: " + phone); } } 
+3
source

You can try with a HashSet .

Public class HashSet extends AbstractSet implements Set, Cloneable, Serializable

  • Values
  • Duplicate not allowed.

Code structure

  HashSet<String> hashSET = new HashSet<String>(); hashSET.add("AA"); hashSET.add("BB"); hashSET.add("CC"); hashSET.add("AA"); // Adding duplicate elements 

Then

 Iterator<String> j = hashSET.iterator(); while (j.hasNext()) System.out.println(j.next()); // Will print "AA" once. } 

Now SORT your HashSet values ​​using TreeSet .

TreeSet implements the SortedSet interface, so duplicate values ​​are not allowed.

  TreeSet<String> _treeSET= new TreeSet<String>(hashSET); 
+3
source

There may be several groups in your contacts, and this group will be WhatsApp, Google, etc. For your contacts and searches that have a whatsApp account . will show double entry with different group

you must use or change your ContactsBean , in a Bean use a HashSet

Note. HashSet can avoid duplicate entries more

HashSet contains only unique elements; it can avoid the same form of a HashSet key element

Bean example

 public class ContactBean { private HashSet<String> number = new HashSet<String>(); public void setNumber(String number) { if (number == null) return; this.number.add(number.trim()); } public HashSet<String> getNumber() { return this.number; } } 

Simple example

 //Creating HashSet and adding elements HashSet<String> hashSet=new HashSet<String>(); hashSet.add("Dhruv"); hashSet.add("Akash"); hashSet.add("Dhruv"); //Avoiding this entry hashSet.add("Nirmal"); //Traversing elements Iterator<String> itr = hashSet.iterator(); while(itr.hasNext()){ System.out.println(itr.next()); } 
+2
source

You can use a HashSet to avoid duplication: -

 HashSet<String> hset = new HashSet<String>(); 

you can add as ArrayList in a HashSet : -

 hset.add(your_string); 

OR

Convert ArrayList to HashSet : -

 Set<String> set = new HashSet<String>(your_arraylist_object); 

HashSet avoid duplicate entries :)

+1
source

I don’t know why you get duplicate elements from contacts, perhaps telephone contacts already have duplicate values. You can check this in the Contacts app.

You should always use the structure of the given data wherever you want to avoid duplication of elements. You can find a better explanation and example here .

+1
source

I think your duplication is due to the fact that the Whatsapp contact is interfering with the contact. so you can use something like this

  String lastPhoneName = ""; String lastPhoneNumber = ""; //IN YOUR CONTACT FETCHING WHILE LOOP , INSIDE TRY String contactName = c.getString(c .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); String phNumber = c .getString(c .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); if (!contactName.equalsIgnoreCase(lastPhoneName) && !phNumber.equalsIgnoreCase(lastPhoneNumber)) { lastPhoneName = contactName; lastPhoneNumber = phNumber; ContactModel model = new ContactModel(); model.setContact_id(contactid); model.setContact_name(contactName.replaceAll("\\s+", "")); model.setContact_number(phNumber.replaceAll("\\D+", "")); list_contact_model.add(model); } 

this will verify that the previous number is the same as the old than skip it. I hope you get an answer

0
source

HashSet add elements to a key / value pair, and also remove a duplicate entry from a set of elements.

 List<String> phone_num_list= new ArrayList<>(); // add elements to phone_num_list, including duplicates Set<String> hs = new HashSet<>(); hs.addAll(phone_num_list); phone_num_list.clear(); phone_num_list.addAll(hs); 

Happy coding !!

0
source

Source: https://habr.com/ru/post/1274019/


All Articles