Skip to content

Instantly share code, notes, and snippets.

@hitenpratap
Last active August 29, 2015 14:10
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/3c6238cf2b7351e8981c to your computer and use it in GitHub Desktop.
Save hitenpratap/3c6238cf2b7351e8981c to your computer and use it in GitHub Desktop.
fetch all Email Addresses associated with particular Contact Id
private Map<String,String> fetchEmails(String id,ContentResolver cr){
Cursor emailDataCursor = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=?", new String[]{id}, null);
if (emailDataCursor.getCount() > 0) {
Map<String,Map<String,String>> emailMap = new HashMap<String, Map<String, String>>();
while (emailDataCursor.moveToNext()) {
Map<String,String> tempEmailMap = new HashMap<String,String>();
String emailData = emailDataCursor.getString(emailDataCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String emailType = emailDataCursor.getString(emailDataCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
String customLabel = emailDataCursor.getString(emailDataCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.LABEL));
CharSequence emailTypeLabel = ContactsContract.CommonDataKinds.Email.getTypeLabel(context.getResources(), emailDataCursor.getInt(emailDataCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)), customLabel);
tempEmailMap.put((String) emailTypeLabel,emailData); //will contain label and email address
emailMap.put(emailType,tempEmailMap); //will contail email Id and map of label and number
}
}
return emailMap;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment