Android get a cursor only with contacts that have

2019-01-07 16:43发布

问题:

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?

回答1:

@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.



回答2:

I solved this one, here is how its done:

UPDATE

   String[] PROJECTION = new String[] { ContactsContract.RawContacts._ID,
        ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.PHOTO_ID,
        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 + " COLLATE NOCASE";
    String filter = Email.DATA + " NOT LIKE '' ) GROUP BY ( " + Email.DATA;

    return mContent.query(Email.CONTENT_URI,
                      PROJECTION, filter, null, order);