Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
168 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...