Android Contacts Display Name and Phone Number(S) in Single Database Query

Android contacts Display Name and Phone Number(s) in single database query?

Try this code:

Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER};

Cursor people = getContentResolver().query(uri, projection, null, null, null);

int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

if(people.moveToFirst()) {
do {
String name = people.getString(indexName);
String number = people.getString(indexNumber);
// Do work...
} while (people.moveToNext());
}

Android get contacts name and number query

@mixkat
I have figured out one more solution.

It is possible to get Name and Phone data using just one query.

Here is the code:

    String WHERE_CONDITION = ContactsContract.Data.MIMETYPE + " = '" +   ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'";
String[] PROJECTION = {ContactsContract.Data.DISPLAY_NAME, ContactsContract.Data.DATA1};
String SORT_ORDER = ContactsContract.Data.DISPLAY_NAME;

Cursor cur = context.getContentResolver().query(
ContactsContract.Data.CONTENT_URI,
PROJECTION,
WHERE_CONDITION,
null,
SORT_ORDER);

In this case you query not Contact provider but Data provider directly.

Retrieve contact on Android with name matching given string and dial that contact's phone number

this code getting all contacts in phone

Cursor phones = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
if (phones != null) {
while (phones.moveToNext()) {
String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();

}

Compare the variable "name" or "phoneNumber" with your String

and you have add permission "android.permission.READ_CONTACTS"



Related Topics



Leave a reply



Submit