0

I am developing an app where i need to get contacts & emails from internal phone book..

After some search in google i found the following code for getting phonenum & name.

Main.java

Cursor numcur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,null, null);
    if (numcur != null) {
        while (numcur.moveToNext()) {
            name = numcur.getString(numcur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            number = numcur.getString(numcur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            phonenumarray.add(number);
            displaynamearray.add(name);

        }
    }

But here to get email id's they are using seperate cursor like as below..

Cursor emailcur = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, null,null, null);
    if (emailcur != null) {
        while (emailcur.moveToNext()) {
            email = emailcur.getString(emailcur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA1));
                emailarray.add(email);  

        }
    }

As here we are getting values seperately when adding values to listview for contact2 it is displayng contact7 mail id and for contact3 it is showing contat5 mail id as show in below figure. enter image description here

So, can anyone help me how to get phone num, mail id for same contact..

2 Answers2

0

try like this

Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
    while (phones.moveToNext()) {
    String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));  
    String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));


    ContactBean objContact = new ContactBean();   
    objContact.setName(name);    
    objContact.setPhoneNo(phoneNumber);   
    list.add(objContact);

see this one How to get the Android device's primary e-mail address

Community
  • 1
  • 1
NagarjunaReddy
  • 8,621
  • 10
  • 63
  • 98
  • Here I am facing issue to get email as the following phone num & display name are working good for me. can you help me with getting email id. –  Mar 15 '13 at 12:07
0

The approach is to use the LOOKUP_KEY as explained in the http://developer.android.com/guide/topics/providers/contacts-provider.html

An example can be found here http://android-er.blogspot.in/2012/12/access-contactscontractcommondatakindsp.html

You can also bring in the CursorJoiner concept for better performance http://asyncindicator.blogspot.in/2012/12/cursorjoiner-and-matrixcursor.html

scout
  • 307
  • 1
  • 3
  • 8