1

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?

marmor
  • 27,641
  • 11
  • 107
  • 150
  • Need more details related to the database as it is not clear from your question. Also you can add extra tags related to database. – Yug Singh Dec 21 '18 at 20:04
  • https://developer.android.com/reference/android/provider/ContactsContract.Contacts use this database – Catherin Yin Dec 21 '18 at 20:15

1 Answers1

0

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

marmor
  • 27,641
  • 11
  • 107
  • 150