Question

I am trying to generate a list of emails from the phone's contacts but I have having trouble specifically getting emails. I can get names and phone numbers but not emails. The ArrayList sizes are both 0 at the end of the loop. Can anyone spot what I am doing wrong, or tell me how emails differ from phone numbers, names and other information?

    ContentResolver cr = getContentResolver();

    Cursor emailCur = cr.query(
            ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
            ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
            null, null);

    ArrayList<String> emails = new ArrayList<String>();
    ArrayList<String> emailTypes = new ArrayList<String>();

    while (emailCur.moveToNext()) {
        // This would allow you get several email addresses
        // if the email addresses were stored in an array
        String email = emailCur
                .getString(emailCur
                        .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
        String emailType = emailCur
                .getString(emailCur
                        .getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));

        emails.add(email);
        emailTypes.add(emailType);

    }

    emailCur.close();

    Log.i("Emails Count", Integer.toString(emails.size()));
    for (int i = 0; i < emails.size(); i++) {
        Log.i("Emails " + i, emails.get(i));
    }

    Log.i("EmailTypes Count", Integer.toString(emailTypes.size()));
    for (int i = 0; i < emailTypes.size(); i++) {
        Log.i("EmailTypes " + i, emailTypes.get(i));
    }

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top