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

后端 未结 2 1724
春和景丽
春和景丽 2020-11-30 07:08

i have the following code to get contacts from content provider

String[] columns = new String[] {
                ContactsContract.Contacts.DISP         


        
2条回答
  •  误落风尘
    2020-11-30 07:37

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

提交回复
热议问题