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

后端 未结 2 1716
春和景丽
春和景丽 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.

    0 讨论(0)
  • 2020-11-30 07:40

    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);
    
    0 讨论(0)
提交回复
热议问题