I\'m trying to obtain a list of contacts from the native database with their Display Name and Phone Number (any or all). There are many methods for obtaining this informatio
Phone numbers are stored in their own table and need to be queried separately. To query the phone number table use the URI stored in the SDK variable ContactsContract.CommonDataKinds.Phone.CONTENT_URI. Use a WHERE conditional to get the phone numbers for the specified contact.
private String displayName(Uri contactUri) {
HashSet detail = ContactDetail.getInstance().getContactArray();
Log.d("ITEM", contactUri.toString());
String[] projection = new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor queryCursor = getActivity().getContentResolver()
.query(contactUri, null, null, null, null);
queryCursor.moveToFirst();
String name = queryCursor.getString(queryCursor.getColumnIndex("display_name"));
String id = queryCursor.getString(
queryCursor.getColumnIndex(ContactsContract.Contacts._ID));
if (Integer.parseInt(queryCursor.getString(queryCursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = getActivity().getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String number = pCur.getString(pCur.getColumnIndex("data1"));
Log.d("Contact Name: ", number);
}
pCur.close();
}
return name;
}
Perform a second query against the Android contacts SQLite database. The phone numbers are queried against the URI stored in ContactsContract.CommonDataKinds.Phone.CONTENT_URI. The contact ID is stored in the phone table as ContactsContract.CommonDataKinds.Phone.CONTACT_ID and the WHERE clause is used to limit the data returned.