1

The following function has a phone number as input parameter (e.g. +436641234567 or +436641234567) and performs two lookups in the Contacts database: first, identify the user belonging to this number (this already works) and then to use the id of the user to get all groups this contact is assigned to (and this does not work). Test gives back the correct id (again), however, the group is "null".

    public String getNameGroupByNumber(Context context, String number) {
    String name = "?";
    String contactId = "0";
    String group = "0";
    String test = "0";

    // Step 1: LookUp Name to given Number
    ContentResolver contentResolver = context.getContentResolver();

    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
    String[] contactProjection = new String[] {BaseColumns._ID, ContactsContract.PhoneLookup.DISPLAY_NAME };
    Cursor contactLookup = contentResolver.query(uri, contactProjection, null, null, null);

    try {
        if (contactLookup != null && contactLookup.getCount() > 0) {
            contactLookup.moveToNext();

            name = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
            contactId = contactLookup.getString(contactLookup.getColumnIndex(BaseColumns._ID));     
        }
    } finally {         
        if (contactLookup != null) {
            contactLookup.close();
        }
    }

    Log.d(TAG, "Name: " +name + " ContactId: " + contactId);    // works as expected

    // Step 2: Lookup group memberships of the contact found in step one
    Uri groupURI = ContactsContract.Data.CONTENT_URI;
    String[] groupProjection = new String[]{ ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID , ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID};
    Cursor groupLookup = contentResolver.query(groupURI, groupProjection, ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID+"="+contactId, null, null);

    try {

        if (groupLookup != null && groupLookup.getCount() > 0) {
            groupLookup.moveToNext();

             test = groupLookup.getString(groupLookup.getColumnIndex(ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID));
             group = groupLookup.getString(groupLookup.getColumnIndex(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID));

             Log.d(TAG, "Group found with Id: " + test + " and GroupId: " + group);   // test is again the contactID from above but group is null

        }
    } finally {         
        if (groupLookup != null) {
            groupLookup.close();
        }
    }

    return name;
 }
Markus Proske
  • 3,356
  • 3
  • 24
  • 32

1 Answers1

3
Cursor groupLookup = getContentResolver().query(
        ContactsContract.Data.CONTENT_URI,
        new String[]{
                ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID , 
                ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID
        },
        ContactsContract.Data.MIMETYPE + "=? AND " + ContactContract.Data.CONTACT_ID + "=?",
        new String[]{
                ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE,
                contact_id
        }, null
);

Use this groupLookup cursor instead.

Cursor groupCursor = getContentResolver().query(
        ContactsContract.Groups.CONTENT_URI,
        new String[]{
                ContactsContract.Groups._ID,
                ContactsContract.Groups.TITLE
        }, ContactsContract.Groups._ID + "=" + _id, null, null
);

groupCursor helps to get Group title from _id.

Sreejith Krishnan R
  • 1,244
  • 10
  • 12
  • Thanks a lot....works out of the box (despite one mission s on ContactSContract). I am a bit confused it returns Group-IDs (e.g. in one example 1, 2, 4) that I do not get when querying the groups with the following code (lowest ID is 3 there): final String[] GROUP_PROJECTION = new String[] { ContactsContract.Groups._ID, ContactsContract.Groups.TITLE }; Cursor cursor = contentResolver.query(ContactsContract.Groups.CONTENT_URI, GROUP_PROJECTION, ContactsContract.Groups.DELETED + "=0", null, ContactsContract.Groups.TITLE); Something with Starred in Android or Google Circles? Any idea? – Markus Proske Dec 31 '12 at 16:17
  • Forgot to mention: those IDs do not show up, when I query groups marked for deletion. – Markus Proske Dec 31 '12 at 16:19
  • 1
    I have edited the answer. I used the `groupCursor` to get the group title. But I haven't checked it with groups with `ContactsContract.Groups.DELETED + "=1"` . It works well with getting group title from groups with `ContactsContract.Groups.DELETED + "=0"`. – Sreejith Krishnan R Dec 31 '12 at 16:51