Stack Exchange
Get email and phone no of name defined without using pick intent
I want to get phone no and email address from contacts using query. Following is my current code. But not getting email from current cursor.
What is wrong in it.
String NAME = "ABC";
    ContentResolver cr = getContentResolver();
    Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
            "DISPLAY_NAME = '" + NAME + "'", null, null);

    if (cursor.moveToFirst()) {
        String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
        String number = "";
        String email = "";

        Cursor cursor_pn_email = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
        while (cursor_pn_email.moveToNext()) {
            number = cursor_pn_email.getString(cursor_pn_email.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            email = cursor_pn_email.getString(cursor_pn_email.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA1));

        }
        cursor_pn_email.close();


    }
    cursor.close();


I got the issue I was finding email using phone cursor. Following is the code for email cursor
      Cursor cursorEmail = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                null,
                ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                new String[]{contactId},
                null);
        if (cursorEmail.moveToFirst()) {
            email = cursorEmail.getString(cursorEmail.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
        }

