3

I am trying to get the list of contacts with their phone and email details to display them in a list. What i have done now is do three separate queries.

  1. Get list of contacts to contactCursor from Contacts.CONTENT_URI
  2. Get all phone numbers to phoneCursor from Phone.CONTENT_ITEM_TYPE
  3. Get all emails to emailCursor from Email.CONTENT_ITEM_TYPE

Then i will am Looping through the contactCursor. For every contact, i will loop through the phoneCursor and emailCursor to get the primary phone number and email. But this takes some time to load and is clearly visible when the number of contacts is large. Is there any way that i can get all these details in the first step itself.

Below is the code snippet:

    Cursor contacts = resolver.query(Contacts.CONTENT_URI, 
            CONTACT_PROJECTION, Contacts.HAS_PHONE_NUMBER + " != 0", null, Contacts._ID + " ASC");
    Cursor phone = resolver.query(Data.CONTENT_URI, PHONE_PROJECTION, 
            Data.MIMETYPE + "=? ", 
            new String[]{Phone.CONTENT_ITEM_TYPE}, 
            Data.CONTACT_ID + " ASC");

    Cursor email = resolver.query(Data.CONTENT_URI, EMAIL_PROJECTION, 
            Data.MIMETYPE + "=? ", 
            new String[]{Email.CONTENT_ITEM_TYPE}, 
            Data.CONTACT_ID + " ASC");

    if (contacts!=null && contacts.moveToFirst()) {
        do {

            String contactId = contacts.getString(0);

            if(phone!=null && phone.moveToFirst()){
                do{
                    if(contactId.equalsIgnoreCase(phone.getString(0))){
                        contact.setMobile(phone.getString(1));

                        break;
                    }
                }while(phone.moveToNext());
            }

            if(email!=null && email.moveToFirst()){
                do{
                    if(contactId.equalsIgnoreCase(email.getString(2))){
                        contact.setEmailWork(email.getString(1));
                        break;
                    }
                }while(email.moveToNext());
            }



        } while (contacts.moveToNext());
arjoan
  • 1,849
  • 2
  • 20
  • 39

1 Answers1

-1

Below is the code to fetch all the contacts including email.

private void getContacts() {
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        String id = null, name = null, email = null, phone = null, note = null, orgName = null, title = null;
        String Phone1 = "unknown", Phone2 = "unknown", Phone3 = "unknown", type1 = "unknown", type2 = "unknown", type3 = "unknown";
        int size = cur.getCount();
        StringBuilder addbuffer = null;
        if (cur.getCount() > 0) {
            int cnt = 1;
            while (cur.moveToNext()) {
                email = "";
                name = "";
                cnt++;
                id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                if (name != null && name != "") {
                    // if (!checkEmail(name)) {
                    // email = "";
                    //
                    // } else {
                    email = name;
                    name = "";
                    // }
                }
                if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                    System.out.println("name : " + name);
                    Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                            new String[] { id }, null);

                    Phone1 = " ";
                    Phone2 = " ";
                    Phone3 = " ";
                    while (pCur.moveToNext()) {
                        String phonetype = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                        String MainNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        if (phonetype.equalsIgnoreCase("1")) {
                            Phone1 = MainNumber;
                            type1 = "home";
                        } else if (phonetype.equalsIgnoreCase("2")) {
                            Phone2 = MainNumber;
                            type2 = "mobile";
                        } else {
                            Phone3 = MainNumber;
                            type3 = "work";
                        }
                    }
                    pCur.close();

                }
                Cursor addrCur = cr.query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI, null, ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID
                        + " = ?", new String[] { id }, null);
                if (addrCur.getCount() == 0) {

                    addbuffer.append("unknown");
                } else {
                    int cntr = 0;
                    while (addrCur.moveToNext()) {

                        cntr++;
                        String poBox = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
                        if (poBox == null) {
                            poBox = " ";
                        }
                        String street = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
                        if (street == null) {
                            street = " ";
                        }
                        String neb = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.NEIGHBORHOOD));
                        if (neb == null) {
                            neb = " ";
                        }
                        String city = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
                        if (city == null) {
                            city = " ";
                        }
                        String state = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
                        if (state == null) {
                            state = " ";
                        }
                        String postalCode = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
                        if (postalCode == null) {
                            postalCode = " ";
                        }
                        String country = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
                        if (country == null) {
                            country = " ";
                        }

                        String type = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
                        if (type == null) {
                            type = " ";
                        }
                    }

                }

                addrCur.close();

                String noteWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
                String[] noteWhereParams = new String[] { id, ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE };
                Cursor noteCur = cr.query(ContactsContract.Data.CONTENT_URI, null, noteWhere, noteWhereParams, null);

                note = " ";

                if (noteCur.moveToFirst()) {
                    note = noteCur.getString(noteCur.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));

                    if (note == null) {
                        note = " ";
                    }
                }
                noteCur.close();
                String orgWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
                String[] orgWhereParams = new String[] { id, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE };
                Cursor orgCur = cr.query(ContactsContract.Data.CONTENT_URI, null, orgWhere, orgWhereParams, null);
                orgName = " ";
                if (orgCur.moveToFirst()) {
                    orgName = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.COMPANY));

                }
                if (orgName == null) {
                    orgName = " ";
                }
                orgCur.close();

                Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                        new String[] { id }, null);
                email = "unknown";
                while (emailCur.moveToNext()) {

                    email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                    String emailType = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));

                    if (email == null) {
                        email = "unknown";
                    }
                    if (emailType.equalsIgnoreCase("1")) {
                    } else {
                    }
                }
                // add
                emailCur.close();
            }
        }
    }
Pratik Dasa
  • 7,439
  • 4
  • 30
  • 44
  • 1
    This involves multiple queries. I want to get all details in a single query. Your code will require 1 + (n x 2) queries for 'n' contacts. – arjoan Sep 06 '13 at 06:46
  • 1
    I also wanted the same, but may be this not possible :( – Uniruddh Feb 05 '14 at 11:37