Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove duplicate contact from contact list in android

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 [email protected],[email protected] for same user so it is returning [email protected]& [email protected] but i want only one record [email protected]

 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;
    }
like image 350
Yogesh Tatwal Avatar asked Nov 07 '13 06:11

Yogesh Tatwal


People also ask

How do I delete duplicates in contacts?

In your list of contacts, hold down Ctrl and click each contact you want to delete. When you've selected all the duplicates, press Delete or Ctrl+D.

Why is my contact list doubled?

Duplicate contacts are really common, and they happen when you're syncing contacts from multiple sources like the cloud, your email, or social media sites.


1 Answers

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.

ArrayList al = new ArrayList();
// add elements to al, including duplicates
HashSet hs = new HashSet();
hs.addAll(al);
al.clear();
al.addAll(hs);
like image 111
Jitender Dev Avatar answered Sep 18 '22 23:09

Jitender Dev