12

please have a look :-

 public static ArrayList<ContactsEntityBean> getContactDetails(
            Context mContext) {
        ArrayList<ContactsEntityBean> contactList = new ArrayList<ContactsEntityBean>();
        ContentResolver cr = mContext.getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                null, null, null);
        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
                                + " = ?", new String[] {
                            id
                        }, null);
                while (cur1.moveToNext()) {
                    ContactsEntityBean contactsEntityBean = new ContactsEntityBean();
                    // to get the contact names
                    String name = cur1
                            .getString(cur1
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

                    // Log.e("Name :", name);
                    String email = cur1
                            .getString(cur1
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

                    // Log.e("Email", email);
                    contactsEntityBean.setName(name);
                    contactsEntityBean.setEmail(email);
                    if (email != null) {
                        contactList.add(contactsEntityBean);
                    }
                }
                cur1.close();
            }
        }
        return contactList;
    }

this method is return multiple contact from same user suppose if i have stored abc@gmail.com,abc@gmail.com for same user so it is returning abc@gmail.com& abc@gmail.com but i want only one record abc@gmail.com

 public static ArrayList<SearchEntityBean> getContactEmailDetails(
            Context mContext) {
        ArrayList<SearchEntityBean> contactList = new ArrayList<SearchEntityBean>();


        try {
            ContentResolver cr = mContext.getContentResolver();
            Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                    null, null, null);
            if (cur.getCount() > 0) {
                while (cur.moveToNext()) {
                    String email = "";
                    String id = cur.getString(cur
                            .getColumnIndex(ContactsContract.Contacts._ID));

                    Cursor cur1 = cr.query(
                            ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Email.CONTACT_ID
                                    + " = ?", new String[] {
                                id
                            }, null);
                    SearchEntityBean contactsEntityBean = new SearchEntityBean();
                    while (cur1.moveToNext()) {

                        // to get the contact names

                        String name = cur1
                                .getString(cur1
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                        String image = cur1
                                .getString(cur1
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_ID));
                        String mail = cur1
                                .getString(cur1
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

                        if (mail != null) {
                            if (!mail.equalsIgnoreCase(LoginPreferenceClass
                                    .getEmailID(mContext)))
                                email = email + mail + ",";
                        }
                        // Log.e("rohit", "Contact  Email :" + email);
                        contactsEntityBean.setName(name);
                        contactsEntityBean.setImage(image);

                    }

                    if (email != null) {

                        if (email.length() > 0) {

                            if (email.split(",").length > 1) {

                                contactsEntityBean.setMutipleEmail(true);

                            }

                            contactsEntityBean.setUserType("2");
                            contactsEntityBean.setContactId(id);
                            contactsEntityBean.setEmail(email);
                            contactList.add(contactsEntityBean);
                        }
                    }
                    cur1.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        HashSet<SearchEntityBean> hs = new HashSet<SearchEntityBean>();
        hs.addAll(contactList);
        contactList.clear();
        contactList.addAll(hs);
        return contactList;
    }
Yogesh Tatwal
  • 2,722
  • 20
  • 43

6 Answers6

5

You should modify your ContactsEntityBean like below

public class ContactsEntityBean {
    private HashSet<String> emails = new HashSet<String>(); 

    public void setEmail(String email) {
        if (email == null)
            return; 
        this.emails.add(email.trim()); 
    }

    public HashSet<String> getEmails() {
        return this.emails; 
    }
}

Will care about duplicate emails... you can use same logic for addresses, phones etc.


Replace your ContactsEntityBean with below code

public class ContactsEntityBean {
    private HashSet<String> emails;
    private HashSet<String> phones;
    private HashSet<String> addresses;
    private String contactId;
    private boolean checked = false;

    public ContactsEntityBean() {
        this.emails = new HashSet<String>();
        this.phones = new HashSet<String>();
        this.addresses = new HashSet<String>();
    }

    public HashSet<String> getPhones() {
        return phones;
    }

    public void setPhones(String phone) {
        if (phone == null)
            return;
        this.phones.add(phone.trim());
    }

    public HashSet<String> getAddresses() {
        return addresses;
    }

    public void setAddresses(String address) {
        if (address == null)
            return;
        this.addresses.add(address.trim());
    }

    public void setEmails(String email) {
        if (email == null)
            return;
        this.emails.add(email.trim());
    }

    public HashSet<String> getEmails() {
        return emails;
    }

    public String getContactId() {
        return contactId;
    }

    public void setContactId(String contactId) {
        this.contactId = contactId;
    }

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }
}

And no need to care about duplicates. this will care about all the things..

Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
5

This is worked form me

private void getContactDetails(ContentResolver contentResolver) {

        Cursor phones = contentResolver.query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
                null, null);
        HashSet<String> mobileNoSet = new HashSet<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(new Contact(name, phoneNumber, email,
                        imagUri, id));
                mobileNoSet.add(phoneNumber);
            }
        }

        adapterContact = new AdapterContact(getActivity(), arrayContacts);
        listContact.setAdapter(adapterContact);
    }
CrazyMind
  • 1,006
  • 1
  • 20
  • 22
2

By changing you method like this your can get the only one contact which is not duplicate..

public static ArrayList<ContactsEntityBean> getContactDetails(Context mContext) {
    ArrayList<ContactsEntityBean> contactList = new ArrayList<ContactsEntityBean>();
    ContentResolver cr = mContext.getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
            null, null, null);
    HashMap<String, String> data = new HashMap<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
                            + " = ?", new String[] { id }, null);
            while (cur1.moveToNext()) {
                ContactsEntityBean contactsEntityBean = new ContactsEntityBean();
                // to get the contact names
                String 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;
}
kalyan pvs
  • 14,486
  • 4
  • 41
  • 59
0

Use HashMap because HashMap does not allow duplicate keys

Declare A Global Variable

// Hash Maps
Map<String, String> nameEmailMap = new HashMap<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 Log
        for (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)

DragonFire
  • 3,722
  • 2
  • 38
  • 51
0

This Solution will:

  • remove duplicates

  • remove all the unnecessary starts i.e currently it is only allowing numbers start with 03, 92 and +92

  • Show all the phone numbers of same name like John has 2 numbers.

     fun fetchContacts(): List<UserObject> {\
              val contacts: ArrayList<UserObject> = ArrayList()
              val mobileNoSet = HashSet<String>()
              val cursor= requiredContext()?.contentResolver?.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,arrayOf(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.PHOTO_URI),null,null,"DISPLAY_NAME ASC")
    
        while (cursor!!.moveToNext()) {
             val index = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)
             val numberIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
             val photoIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI)
             val strName = cursor.getString(index)
             var number = cursor.getString(numberIndex)
             number = number.replace(" ", "")
             val photo = cursor.getString(photoIndex)
             if ((number.startsWith("03") || number.startsWith("92")||number.startsWith("+92")) && !mobileNoSet.contains(number)) {
                    contacts.add(UserObject(photo,strName,number))mobileNoSet.add(number)
                }
         }
        return contacts
      }
    
Dharman
  • 30,962
  • 25
  • 85
  • 135
Nouman Ch
  • 4,023
  • 4
  • 29
  • 42
-1

Once a contact email added in your ArrayList put continue after that and use HashSet instead of ArrayList

if (email != null) {
    contactList.add(contactsEntityBean);
    continue;
}
Sunny
  • 14,522
  • 15
  • 84
  • 129
  • can you answer this similar question ? http://stackoverflow.com/questions/25801533/what-is-the-best-way-to-get-distinct-contacts-in-android – Vivek Warde Sep 12 '14 at 07:20