1

In my app I read all the contacts from my phone into list view. After clicking on a particular view from the list, I want to read the name, phone and e-mail address. I manage to read the name and phone without a problem. But I can not get the email.

Say I have 2 contacts:

Bill
55-555-555
bil@example.com

Mark
66-666-666
mark@example.com

So the field of the email is called reverse. For Bill I get Mark's email And for Mark I get Bill's email. Other values ​​are correct.

private  HashMap<String,String> getContactNames() 
{
        HashMap<String,String>contact=new HashMap<>();
        ContentResolver cr = getContentResolver();
        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        cursor.moveToPosition(pos_listView);
        String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        phones.moveToPosition(pos_listView);
        String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

        Cursor emailcur = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, null,null, null);
        emailcur.moveToPosition(pos_listView);
        String email = emailcur.getString(emailcur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA1));

        contact.put("name",name);
        contact.put("phoneNumber",phoneNumber);
        contact.put("email",email);

        cursor.close();
        return contact;
    }
marmor
  • 27,641
  • 11
  • 107
  • 150
Haim cohen
  • 11
  • 2

1 Answers1

0

Nope, that's definitely not the way it works.

There are 3 different tables in ContactsContract APIs:

  1. Contacts
  2. RawContacts
  3. Data (which you can access either directly via Data.CONTENT_URI, or through helper sub-tables via Phone.CONTENT_URI, Email.CONTENT_URI, etc.)

The position of a contact within the list of contacts has nothing to do with the position if its phone or emails within their tables, so moveToPosition(pos_listView) is very wrong.

This is what you should do:

  1. When reading the list of all contacts, keep track of their contact-id's via Contacts._ID and make sure you can get it when a contact is clicked
  2. When a contact is clicked, get its contact-id, and send it to your method.
  3. In your contact info method, query only once for all the phones and emails (and possibly more info if you need) to get them all at once.

Your method would then look something like this:

private void getContactInfo(long contactId) {
    String[] projection = {Data.DISPLAY_NAME, Data.MIMETYPE, Data.DATA1};
    String selection = Data.CONTACT_ID + "=" + contactId + " AND " + Data.MIMETYPE + " IN ('" + Phone.CONTENT_ITEM_TYPE + "', '" + Email.CONTENT_ITEM_TYPE + "')";
    Cursor cur = cr.query(Data.CONTENT_URI, projection, selection, null, null);

    Log.i(TAG, "dumping info for contact id: " + contactId);
    while (cur.moveToNext()) {
        String name = cur.getString(0);
        String mime = cur.getString(1); // type of data: email, phone, company, etc.
        String data = cur.getString(2); // the actual info, e.g. +1-212-555-1234

        String type = Phone.CONTENT_ITEM_TYPE.equals(mime) ? "phone" : "email";

        Log.i(TAG, "info: " + name + " - " + type + ": " + data);
    }
    cur.close();
}
marmor
  • 27,641
  • 11
  • 107
  • 150