Naga Lokesh Kannumoori
Naga Lokesh Kannumoori

Reputation: 603

How to get contact name, phone number, email details using Loaders

we can get contact name and phone number using this uri

ContactsContract.CommonDataKinds.Phone.CONTENT_URI

and we can get email address using uri

ContactsContract.CommonDataKinds.Email.CONTENT_URI

but, how can i pass these both uri's to loader and retuen Cursor loader as in code. in this code i'm returning only name and number but how can i get email to get that how can i pass

ContactsContract.CommonDataKinds.Email.CONTENT_URI

to it

return new CursorLoader(this,
                 ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                projection,
                null,
                null,
                null);

Upvotes: 0

Views: 507

Answers (1)

marmor
marmor

Reputation: 28199

Both CommonDataKinds.Phone and CommonDataKinds.Email are actually part of a large table ContactsContract.Data, that table contains lots of other info you might not be interested in, so you can select only the items that interest you (phones and emails) using Data.MIMETYPE.

So the CursorLoader initialization can be something like this:

new CursorLoader(this,
    ContactsContract.Data.CONTENT_URI,
    projection,
    ContactsContract.Data.MIMETYPE + " IN (" + CommonDataKinds.Phone.CONTENT_TYPE + ", " + CommonDataKinds.Email.CONTENT_TYPE + ")",
    null,
    null);

However, note that like you're getting one like per phone and not per contact when querying just on CommonDataKinds.Phone.CONTENT_URI, you'll now be getting one row per phone or email, and not aggregated by contact.

You should add Data.MIMETYPE to your projection, and use that to determine if the current row is a phone row or an email row.

Upvotes: 1

Related Questions