0

I have succeded in getting the ID and name from a phone number that is calling in. What I would like to to is to see which groups this ID belongs to. I have tried the following:

    //Search for the information about the phone number, save the goupID(s)
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(aNumber)); 
    ContentResolver cr = mService.getContentResolver();
    Cursor myCursor = cr.query(uri, new String[]{PhoneLookup._ID, PhoneLookup.DISPLAY_NAME},null, null, null);

    myCursor.moveToFirst();
    //String contactID = myCursor.getString(myCursor.getColumnIndex(PhoneLookup._ID)); 
    String contactID = myCursor.getString(myCursor.getColumnIndex(ContactsContract.Contacts._ID)); 
    myCursor.close();

    //Use the cursor to query for group with help of ID from the Phone look up
    myCursor = cr.query(ContactsContract.Groups.CONTENT_URI,
            new String[]{ContactsContract.Groups._ID},
            ContactsContract.Groups._ID + " = ?",
            new String[]{contactID}, 
            null);      

    //Contact may be in more than one group
    nbrOfGroups = myCursor.getCount(); 
    groupName = new String [nbrOfGroups];

The problem is tha second query, where I would like to use the contactID that i found in the phone lookup, to see which groups that contacID belongs to. The result is no group, although the contact is added to a group in my contacs.

Any ideas? :)

  • just a suggestion: you DEFINITELY want to be using ContentProviders here. They are designed specifically to access this information safely and atomically for you. Take a look [here](http://developer.android.com/reference/android/content/ContentProvider.html) for more info. :) – Codeman Jan 03 '12 at 18:45

1 Answers1

1

Groups._ID is not same as Contact ID, instead it is the index for the table storing all the group information. After you get the contact ID you should get all the group membership for that contact from the data table by using the Group membership mimetype.

After getting the group ids you can query the Groups table to get the TITLE for all the groups

try with this code

    //Search for the information about the phone number, save the goupID(s)
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode("123123")); 
    ContentResolver cr = this.getContentResolver();
    Cursor myCursor = cr.query(uri, new String[]{PhoneLookup._ID, PhoneLookup.DISPLAY_NAME},null, null, null);

    myCursor.moveToFirst();
    //String contactID = myCursor.getString(myCursor.getColumnIndex(PhoneLookup._ID)); 
    String contactID = myCursor.getString(myCursor.getColumnIndex(ContactsContract.Contacts._ID)); 
    myCursor.close();

    //Use the cursor to query for group with help of contact ID from the Phone look up
    myCursor = cr.query(ContactsContract.Data.CONTENT_URI,
            new String[]{ContactsContract.Data.CONTACT_ID, 
            ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID},
            ContactsContract.Data.CONTACT_ID + " = ? " + 
            Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE + "'",
            new String[]{contactID}, 
            null);      

    //Contact may be in more than one group
    int nbrOfGroups = myCursor.getCount();
    int[] groupIds = new int[nbrOfGroups];
    int index = 0;

    // unfortunately group names are stored in Groups table
    // so we need to query again
    if (myCursor.moveToFirst()) {
        do {
            groupIds[index] = myCursor.getInt(1); // Group_row_id column
        } while (myCursor.moveToNext());
    }

    myCursor.close();

    // construct the selection
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < nbrOfGroups; i++) {
        if (i != 0) {
            sb.append(",");
        }

        sb.append(groupIds[i]);
    }

    String[] groupName = new String [nbrOfGroups];
    myCursor = cr.query(ContactsContract.Groups.CONTENT_URI,
            new String[]{ContactsContract.Groups.TITLE},
            ContactsContract.Groups._ID + " IN (" + sb.toString() + ")",
            null, null);

    // finally got the names
    if (myCursor.moveToFirst()) {
        do {
            groupName[index] = myCursor.getString(0); // Group_row_id column
        } while (myCursor.moveToNext());
    }

    myCursor.close();
Ankur Kumar
  • 972
  • 7
  • 12