Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Nope, you aren't on the verge of anything wrong. The Contacts ContentProvider (and pretty much every ContentProvider I've seen) contradicts conventional SQL wisdom, oh well.</p> <p>If you want to get data from multiple types of ContactsContracts.CommonDataKinds, you need to detect the mime-type of the row you're in and determine what kind of entity it is (or make an extra query, my preferred solution is to do it all in one query...) </p> <p>Hold your breath, here's an example of some code I wrote to do such a thing. You should be able to see how the different rows can be different kinds of entities and as a result of that their generic columns can hold different kinds of data, which is why we use CommonDataKinds.* classes to reference them contextually:</p> <pre><code>public class ContactsHelper { private static String[] PROJECTION = { Data.CONTACT_ID, Data.MIMETYPE, StructuredName.GIVEN_NAME, StructuredName.FAMILY_NAME, StructuredName.DISPLAY_NAME, StructuredName.MIDDLE_NAME, Email.ADDRESS }; public static void readContacts(Context context) { ContentResolver resolver = context.getContentResolver(); Uri contactsUri = ContactsContract.Data.CONTENT_URI; SQLiteDatabase conn = DatabaseHelper.openDatabase(context); Cursor cursor = resolver.query(contactsUri, PROJECTION, null, null, null); int idxContactId = cursor.getColumnIndex(Data.CONTACT_ID); int idxMimeType = cursor.getColumnIndex(Data.MIMETYPE); int idxGivenName = cursor.getColumnIndex(StructuredName.GIVEN_NAME); int idxFamilyName = cursor.getColumnIndex(StructuredName.FAMILY_NAME); int idxDisplayName = cursor.getColumnIndex(StructuredName.DISPLAY_NAME); int idxMiddleName = cursor.getColumnIndex(StructuredName.MIDDLE_NAME); int idxEmail = cursor.getColumnIndex(Email.ADDRESS); for (cursor.moveToFirst(); ! cursor.isAfterLast(); cursor.moveToNext()) { String mimeType = cursor.getString(idxMimeType); Integer contactId = cursor.getInt(idxContactId); if (StructuredName.CONTENT_ITEM_TYPE.equals(mimeType)) { String firstName = cursor.getString(idxGivenName); String middleName = cursor.getString(idxMiddleName); String lastName = cursor.getString(idxFamilyName); String displayName = cursor.getString(idxDisplayName); Log.d(TAG, all the values ^^^); } if (Email.CONTENT_ITEM_TYPE.equals(mimeType)) { String emailName = cursor.getString(idxEmail); Log.d(TAG, all the values ^^^); } } DatabaseHelper.closeDatabase(conn); cursor.close(); } } </code></pre> <p>Yes this completely serious solution does use string comparisons. If there is a better way, please let me know!</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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