0

Please, I've been trying all day to get the email address of all the contact on a phone but somehow, i can't get it to work. Please can someone tell me where am going wrong.. all others work except getting the email. Thanks in advance

Cursor cursor = getContentResolver().query(   ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,null, null);

    //now we have cusror with contacts and get diffrent value from cusror.

    while (cursor.moveToNext()) {
        String id = cursor.getString(cursor
                .getColumnIndex(ContactsContract.Contacts._ID));
    String name =cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

    contactView.append("Name: ");
    contactView.append(name);
    contactView.append("\n");

     if (Integer
             .parseInt(cursor.getString(cursor
                     .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
    String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    contactView.append("Number: ");
    contactView.append(phoneNumber);
    contactView.append("\n");}
    //get email cursor
    Cursor ecursor = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,  ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = "+id, 
            null, null);

   while (ecursor.moveToNext()) {
        String email = ecursor.getString(ecursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
        if(email != null){
            contactView.append("email: ");
            contactView.append(email);
            contactView.append("\n");
            }

    }
    ecursor.close();
    }
    cursor.close();
uchman21
  • 678
  • 3
  • 6
  • 22
  • I tried it and it works! I can find the email of a contact, but only if the contact has a phone number. If a contact has only a name and an email address, your code doesn't show it. – user2340612 May 16 '13 at 18:17
  • it only shows me the name and phone number without the email\ – uchman21 May 16 '13 at 18:41

0 Answers0