Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Contact list has duplicate names

I have a contact list in a sort order. But in my contact list the name is duplicating with same number. I think the issue is because of the contact list sync with different account.

I check with Hash map. But when I using hash map the result is not sorted with name .

private static final String[] PROJECTION = new String[] {
    ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
    ContactsContract.Contacts.DISPLAY_NAME,
    ContactsContract.CommonDataKinds.Phone.NUMBER
};

ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION,
  null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE NOCASE ASC");

if (cursor != null) {
    try {
        int nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
        String nameContact = cursor.getString(nameIndex);
    finally {
        cursor.close();
    }
}

Adapter

holder.name.setText(itemListPogo.get(position).getItemName());

Can anyone please help to avoid the duplication in name.

like image 481
Binil Surendran Avatar asked Dec 08 '15 07:12

Binil Surendran


People also ask

Why are contacts showing twice on Android?

Sometimes your phone creates two or more than two copies of a single contact. This mostly happens when you factory reset a device and sync contacts or change SIM and accidentally sync all contacts. This can completely clutter the contacts, making it hard to navigate through contacts.

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.

How do I stop my phone from duplicating contacts?

To disable the auto-sync, tap the toggle next to Automatically back up & sync device contacts to the Off position (grey or white). To enable the auto-sync, tap the toggle next to Automatically back up & sync device contacts to the On position (blue or green).


Video Answer


1 Answers

You're seeing duplicate contacts because they belong to different accounts. i.e. Same number can show up 3 times if it is synced with Facebook, WhatsApp, and Google account. You can find more information here Android Account Manager

This is how you can use column ContactsContract.RawContacts.ACCOUNT_TYPE to filter and retrieve contacts associated with a single account only.

String[] projection = new String[] {
                    ContactsContract.RawContacts._ID,
                    ContactsContract.RawContacts.ACCOUNT_TYPE,
                    ContactsContract.Contacts.DISPLAY_NAME,
                    ContactsContract.CommonDataKinds.Phone.PHOTO_URI,
                    ContactsContract.CommonDataKinds.Phone.NUMBER,
                    ContactsContract.CommonDataKinds.Photo.CONTACT_ID };


            String selectionFields =  ContactsContract.RawContacts.ACCOUNT_TYPE + " = ?";
            String[] selectionArgs = new String[]{"com.google"};

            Cursor cursor =  getContentResolver().query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    projection,
                    selectionFields,
                    selectionArgs,
                    ContactsContract.Contacts.DISPLAY_NAME
            );

In this code, only contacts that are associated with Google Account are selected. Similarly, if you want to get list of WhatsApp Contacts only you can replace "com.google" with "com.whatsapp"

like image 172
Zain Ul Abideen Avatar answered Oct 27 '22 15:10

Zain Ul Abideen