I am fetching all the contact groups using the following query:
//get list of contact groups from ContactsContract
Cursor groups_cursor = getContentResolver()//
.query(ContactsContract.Groups.CONTENT_SUMMARY_URI,//
new String[] { //
ContactsContract.Groups._ID,//
ContactsContract.Groups.TITLE, //
ContactsContract.Groups.DATA_SET,//
ContactsContract.Groups.GROUP_VISIBLE,//
ContactsContract.Groups.DELETED,//
ContactsContract.Groups.SYSTEM_ID,//
ContactsContract.Groups.SUMMARY_COUNT, //
ContactsContract.Groups.SUMMARY_WITH_PHONES //
},//
null, null, null);//
My cursor always contains the groups "Family", "Friends" and "Coworkers" twice.
Why do I get these duplicates?
I have a single Google Account synced.
Groups are listed correctly in the Contacts app.
I've had a similar problem. On my device, I note that:
- for each pair of duplicate groups, one group always contains 0 contacts (
ContactsContract.Groups.SUMMARY_COUNT
), and
- all contact groups with > 0 contacts have a non-empty
ContactsContract.Groups.NOTES
column.
It's still not clear to me why the empty groups are returned, but I've got around it by selecting non-empty groups, or those with non-empty notes columns.
Hope this helps!
I've found that all of these empty, duplicate groups have ACCOUNT_TYPE = "DeviceOnly", whereas the legitimate groups have ACCOUNT_TYPE = "com.google". So if you add that as a parameter you should be good!
Here is my selection statement
private static final String CONTACT_GROUP_SELECTION = ContactsContract.Groups.AUTO_ADD + " = 0 "
+ " AND " + ContactsContract.Groups.ACCOUNT_TYPE + " = 'com.google' "
+ " AND " + ContactsContract.Groups.ACCOUNT_NAME + " NOT NULL "
+ " AND " + ContactsContract.Groups.FAVORITES + " = 0 "
+ " AND " + ContactsContract.Groups.DELETED + " = 0 ";
The other clauses were added because an internal Android library uses them, and it seemed like a good idea. But my problem of duplicate groups didn't go away until I added the "com.google" clause.
This is an old question but since there's still no correct answer here's mine:
- Groups can be deleted
- Groups can be invisible (used for internal use only)
- Groups can have different origins (phone contacts, different sync adapters = Google accounts or other accounts in general)
The first two can be easily be filtered using:
ContactsContract.Groups.DELETED + " = 0 AND " +
ContactsContract.Groups.GROUP_VISIBLE + " = 1";
The origin should probably not be filtered at all. I'm using several Google accounts on my device and end up having multiple "Starred in Android" and "My Contacts" groups which were synced by the Google sync adapter for the different accounts.
The best solution is probably to merge groups with the same name. The user usually doesn't care what account the contacts/groups were synced from.
I faced same issue (on Android 5.1 on LG G4) and all of the answers were false.
GROUP_VISIBLE = '1'
doesn't return all groups which are visible in Contacts application.
NOTES
also doesn't give any reliable information, because all of the groups has some note.
ACCOUNT_TYPE
- also false, because I added my group, and it appear twice - one with com.google
type, and another with lg.sync
but some of the com.google
groups were non empty, and some of the lg.sync
groups also were not empty.
It looks like only reliable way to query contact groups is to select non-empty groups. And if your requirement is to display also empty groups, there is nothing you can do, except displaying both.
Interesingly, on MotoG 1st gen Android 5.1 everything seems to work fine, with no duplicated groups. Only GROUP_VISIBLE
works bad, because it doesn't show custom groups at all.