how to get all Phone.DISPLAY_NAME_PRIMARY and phone number size onece

189 Views Asked by At

I want to get all contact name along with phone number size in it. Now I am using tow queries:

1.

crContacts = getContentResolver()
            .query(ContactsContract.Contacts.CONTENT_URI, null,null, null, null)

and then 2.

while (crContacts.moveToNext()) {
id = crContacts.getString(crContacts.getColumnIndex(ContactsContract.Contacts._ID));
Cursor crPhones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = ?", new String[]{id}, null);

}

Any way to get phone number size and Phone.DISPLAY_NAME_PRIMARY once without query twice?

1

There are 1 best solutions below

0
marmor On

Not sure what you mean by phone number size, but you can easily access all Phone.CONTENT_URI fields along with implicit joins from the Contacts.CONTENT_URI table, here's an example:

String[] projection = new String[] { Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER };
Cursor c = cr.query(Phone.CONTENT_URI,projection,null,null,null);
while (c.moveToNext()) {
   long contactId = c.getLong(0);
   String name = c.getString(1);
   String phone = c.getString(2);
   Log.i("Phones", "got contact phone: " + contactId + " - " + name + " - " + phone);
}

You can read more about the implicit joins you get when querying any Data table here: https://developer.android.com/reference/android/provider/ContactsContract.Data#columns