Skip to content

Instantly share code, notes, and snippets.

@hitenpratap
Created December 2, 2014 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hitenpratap/5d3872f922ea792990a3 to your computer and use it in GitHub Desktop.
Save hitenpratap/5d3872f922ea792990a3 to your computer and use it in GitHub Desktop.
fetch all Phone numbers associated with particular Contact Id
private Map<String,String> fetchPhones(String id,ContentResolver cr){
Cursor phoneDataCursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?", new String[]{id}, null);
if (phoneDataCursor.getCount() > 0) {
Map<String,Map<String,String>> phoneMap = new HashMap<String, Map<String, String>>();
while (phoneDataCursor.moveToNext()) {
Map<String,String> tempPhoneMap = new HashMap<String,String>();
String phoneData = phoneDataCursor.getString(phoneDataCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
String phoneType = phoneDataCursor.getString(phoneDataCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
String customLabel = phoneDataCursor.getString(phoneDataCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL));
CharSequence phoneTypeLabel = ContactsContract.CommonDataKinds.Phone.getTypeLabel(context.getResources(), phoneDataCursor.getInt(phoneDataCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)), customLabel);
tempPhoneMap.put((String) phoneTypeLabel,phoneData); //will contain label and number
phoneMap.put(phoneType,tempPhoneMap); //will contail phone Id and map of label and number
}
}
return phoneMap;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment