I followed this link and got a working code to get names and e-mails of contacts who's emails exist (only those with e-mail address). However, i'd like to add phone number there aswell, if one exists. Currently i'm still learning and can't quite grasp the whole concept of Cursors and why am i not able to get phone number with my code
private void displayContacts() {
ArrayList<String> namesmails = getNameEmailDetails();
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
int counter = 0;
for (int i = 0; i<namesmails.size();i = i+2) {
//namesmails[0] == name && namesmails[1] == phone && namesmails[2] == email
String name = namesmails.get(i);
String mail = namesmails.get(i+1);
listDataHeader.add(name);
List<String> currentName = new ArrayList<String>();
currentName.add(mail);
listDataChild.put(listDataHeader.get(counter), currentName);
counter += 1;
}
}
/*
This method gathers the information about contacts
*/
public ArrayList<String> getNameEmailDetails(){
ArrayList<String> names = new ArrayList<String>();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
Cursor cur1 = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (cur1.moveToNext()) {
//to get the contact names
String name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
Log.e("Name :", name);
String phone=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.e("Nr: ",phone); //Here is e-mail instead of phone nr?!?!
String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Log.e("Email", email);
if(email!=null){
names.add(name);
names.add(email);
}
}
cur1.close();
}
}
return names;
}
As i pointed out with a comment, the contact phone number is not displayed, however the e-mail is. What should i change so the contact phone would also be displayed.