Android get a cursor only with contacts that have an email listed >android 2.0

DArkO picture DArkO · Mar 5, 2011 · Viewed 12.1k times · Source

i have the following code to get contacts from content provider

String[] columns = new String[] {
                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.Contacts._ID,
                ContactsContract.Contacts.PHOTO_ID };
        Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI,
                columns, null, null, null);

and i use this one to get the emails for a specific contact by their id:

Cursor emails = getContentResolver().query(
                    ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Email.CONTACT_ID
                            + " = " + contact.getContactId(), null, null);

my current implementation passes every row in the cursor and aquires its emails and stores them in an arrayList of java objects.

what i was wondering if it was possible to do is just query the content provider and return a cursor of just contacts with ids/name etc that have an email address listed.

this way has a long waiting period for getting the contact list. i am using this list for a list adapter. if i can get only the contacts that have an email i can use a cursor adapter in my list.

Is something like this possible? how can i speed up the process?

Answer

Marcin picture Marcin · Apr 4, 2013

@CapDroid

Fixed working code from DArkO's post:

    ContentResolver cr = context.getContentResolver();
    String[] PROJECTION = new String[] { ContactsContract.RawContacts._ID, 
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.PHOTO_ID,
            ContactsContract.CommonDataKinds.Email.DATA, 
            ContactsContract.CommonDataKinds.Photo.CONTACT_ID };
    String order = "CASE WHEN " 
            + ContactsContract.Contacts.DISPLAY_NAME 
            + " NOT LIKE '%@%' THEN 1 ELSE 2 END, " 
            + ContactsContract.Contacts.DISPLAY_NAME 
            + ", " 
            + ContactsContract.CommonDataKinds.Email.DATA
            + " COLLATE NOCASE";
    String filter = ContactsContract.CommonDataKinds.Email.DATA + " NOT LIKE ''";
    Cursor cur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, PROJECTION, filter, null, order);

Your cursor will have essential IDs as well as names and email addresses. The performance of this code is great because it requests few columns only.