Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid 4.1+/ContactContract: Lookup group to given contactId (derived from lookup a number)
    primarykey
    data
    text
    <p>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 <strong>get all groups</strong> this contact is assigned to (and this does not work). Test gives back the correct id (again), however, the group is "null".</p> <pre><code> 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 &amp;&amp; contactLookup.getCount() &gt; 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 &amp;&amp; groupLookup.getCount() &gt; 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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload