Reputation: 97
I tried to get all group from android device with below code. Then I put all group name and id in arraylist.
Uri CONTENT_URI = ContactsContract.Groups.CONTENT_URI;
String GR_ID = ContactsContract.Groups._ID;
String GR_TITLE = ContactsContract.Groups.TITLE;
ContentResolver cr = getContentResolver();
Cursor groupCursor = cr.query(CONTENT_URI, null, null, null,
ContactsContract.Groups._ID + " ASC");
while (groupCursor.moveToNext()) {
String group_id = groupCursor.getString(groupCursor.getColumnIndex(GR_ID));
String group_name = groupCursor.getString(groupCursor.getColumnIndex(GR_TITLE));
Group grp = new Group(group_name);
grp.setId(group_id);
arrayPersonal.add(grp);
}
groupCursor.close();
This is result
Some group have different id but same name. I dont know why? Any help would be much appreciated! Thanks!
Upvotes: 0
Views: 139
Reputation: 469
I had the same problem, and I solved it by selecting where ContactsContract.Groups.ACCOUNT_TYPE = 'com.google'
I came across this because I queried for the ACCOUNT_TYPE column to see what it contained, and some of the entries were "com.google" and others were "DeviceOnly". I have some other constraints on my group query, taken from an internal Android library (GrepCode), thus it seems wise to use them. Here is my entire selection statement (for the CursorLoader)
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 ";
Upvotes: 1