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
Session 'App': Error Launching Activity
Textinputlayout Not Showing Edittext Hint Before User Focus on It
How to Write Style to Error Text of Edittext in Android
Get Focused View from Viewpager
Moving and Vanishing Lines of Code; Trouble with Eclipse's Xml Editor
How to Get Phone Number from an Incoming Call
Android - Switch Tabs from Within an Activity Within a Tab
Android:Select Image from Gallery Then Crop That and Show in an Imageview
Http Connection Timeout on Android Not Working
Programmatically Add Id to R.Id
Custom Dialog on Android: How to Center Its Title
Detect Connectivity Change in Android 7 and Above When App Is Killed/In Background
Android Error - Close() Was Never Explicitly Called on Database
Android Volley Double Post When Have Slow Request
How to Get Selected Items from Multi Select List View