0

I am fetching all the contacts of my phone and showing them in listview. All the other required details are shown properly but I am having issue in fetching email address. I have many contacts which have email address associated with them but it doesn't email address for any contacts. I have matched my email query with other SO questions and I am also having same query as them. Am I missing something?

Here is my query

if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Email.DATA ) ) {
                            switch(dataCursor.getInt(dataCursor.getColumnIndex("data2"))){
                                case ContactsContract.CommonDataKinds.Email.TYPE_HOME :
                                    homeEmail = dataCursor.getString(dataCursor.getColumnIndex("data1"));
                                    break;
                                case ContactsContract.CommonDataKinds.Email.TYPE_WORK :
                                    workEmail = dataCursor.getString(dataCursor.getColumnIndex("data1"));
                                    break;
                            }
                        }

Cursor dataCursor = getContentResolver().query(dataUri, null,
                        ContactsContract.Data.CONTACT_ID + "=" + contactId,
                        null, null);
Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84

1 Answers1

0

Above query that I posted doesn't work. I found another query which is working for me.

Cursor emails = getContentResolver().query(
                        ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Email.CONTACT_ID
                                + " = " + contactId, null, null);
                while(emails.moveToNext()) {
                    String emailAddress = emails
                            .getString(emails
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                }emails.close();
Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84