1

I am working on an application that includes dealing with Contacts also. I have tried distinct methods But none of them worked.. I am fetching contact details in one of the tabbed Activity fragments. Its fetching all other details correctly except Email Address of the contact. I have figured out the reason because ContactsContract.CommonDataKinds.Phone._ID fetching different Ids than ContactsContract.CommonDataKinds.Email.CONTACT_ID.. Now what should i code to fetch all the contacts from phone along with name, number, email, photo_uri. Please Help! Here is my piece of code:

    public class FragmentContacts extends Fragment{

ListView contacts;
ArrayList<String> namelist=new ArrayList<String>();
ArrayList<String> number=new ArrayList<String>();
ArrayList<String> contact_id=new ArrayList<String>();
ArrayList<String> contact_image_uri=new ArrayList<String>();
ArrayList<String> contact_email=new ArrayList<String>();
String[] sortImage;
ImageButton addContact;
String email;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_contacts, container, false);
    contacts=(ListView)rootView.findViewById(R.id.contact_list);
    addContact=(ImageButton)rootView.findViewById(R.id.add_contact);
    namelist.clear();
    getContacts();

    addContact.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            addNewContact();
        }
    });
    return rootView;
}


public void getContacts() {
    ContentResolver cr = getActivity().getApplicationContext().getContentResolver();

    Cursor managedCursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            new String[]{ContactsContract.CommonDataKinds.Phone._ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.PHOTO_URI}, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE NOCASE ASC");

    while (managedCursor.moveToNext()) {

        String id = managedCursor.getString(0);
        String name = managedCursor.getString(1);
        String phoneNumber = managedCursor.getString(2);
        String image_uri=managedCursor.getString(3);
        //email=managedCursor.getString(4);
        email=getEmail(id);
        System.out.println("Email is "+email);
        contact_id.add(id);
       // System.out.println("Id is "+id+" "+"Name is "+name);
        namelist.add(name);
        number.add(phoneNumber);
        contact_image_uri.add(image_uri);
        contact_email.add(email);
    }
    managedCursor.close();



    ContactAdapter a = new ContactAdapter(getActivity(), contact_id, namelist, number,contact_image_uri,contact_email);
    contacts.setAdapter(a);
}


public void addNewContact()
{
    Intent in= new Intent(getContext(),AddContact.class);
    startActivity(in);
}

private String getEmail(String contactId) {

    String mailE=null;
    ContentResolver cr = getActivity().getApplicationContext().getContentResolver();
    Cursor cursor = cr.query(
            ContactsContract.CommonDataKinds.Email.CONTENT_URI,
            null,
            ContactsContract.CommonDataKinds.Email.CONTACT_ID +" = ?",
            new String[]{contactId}, null);

    while (cursor.moveToNext())
    {
        mailE=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
        System.out.println("In Between Email is "+mailE);
    }

    cursor.close();
    return mailE;
}
 }

4 Answers4

0

For Getting Email Address Email,

 String email = managedCursor .getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

Try this.

rak
  • 108
  • 1
  • 12
0

You can you this method to get the email ID of that contact. In your case you can pass the contact ID directly.

public ArrayList<String> getEmail() {
        ArrayList<String> names = new ArrayList<String>();
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                Cursor cur1 = cr.query( 
                        ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", 
                                new String[]{id}, null); 
                while (cur1.moveToNext()) { 
                    //to get the contact names
                    String name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    Log.e("Name :", name);
                    String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                    Log.e("Email", email);
                    if(email!=null){
                        names.add(name);
                    }
                } 
                cur1.close();
            }
        }
        return names;
    }
Akanksha Hegde
  • 1,738
  • 11
  • 14
0

There's some confusion in your code, you can't query over Phone.CONTENT_URI and expect to get an email-address, as emails are stored in Email.CONTENT_URI.

The naive way would be to add another query over Email.CONTENT_URI, but that's not needed, your best choice would be to query over the entire Data.CONTENT_URI table which contains both emails and phones (and a lot of other types of information), and ask for only mimetypes for emails and phones.

String[] projection = new String[] { Data.CONTACT_ID, Data.DISPLAY_NAME, Data.MIMETYPE, Email.ADDRESS, Phone.NUMBER };
Cursor cur = getContentResolver().query(Data.CONTENT_URI, projection, Data.MIMETYPE + " IN ('" + Email.CONTENT_ITEM_TYPE + "' , '" + Phone.CONTENT_ITEM_TYPE +"')", null, null);

while ((cur != null) && cur.moveToNext()) {
    long contactId = cur.getLong(0);
    String name = cur.getString(1);
    String type = cur.getString(2);
    String email = null;
    String phone = null;
    if (type == Email.CONTENT_ITEM_TYPE) {
      email = cur.getString(3);
    } else {
      phone = cur.getString(4);
    }
    // do whatever you want with above variables.
}

Note: you'll get the same contactId and name for each phone/email stored for that contact, so you should use a HashMap to map contactId to list of details, in order to store all this data.

You should definitely read first to docs about the ContactsContract API, and Contacts and Data tables, as these are crucial to understanding how to query for information about contacts.

marmor
  • 27,641
  • 11
  • 107
  • 150
0

Finally i'v resolved it. I have changed contact_id from ContactsContract.CommonDataKinds.Phone._ID to ContactsContract.CommonDataKinds.Phone.CONTACT_ID... My Updated code is:

public class FragmentContacts extends Fragment{

ListView contacts;
ArrayList<String> namelist=new ArrayList<String>();
ArrayList<String> number=new ArrayList<String>();
ArrayList<String> contact_id=new ArrayList<String>();
ArrayList<String> contact_image_uri=new ArrayList<String>();
ArrayList<String> contact_email=new ArrayList<String>();
ImageButton addContact;
String email;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_contacts, container, false);
    contacts=(ListView)rootView.findViewById(R.id.contact_list);
    addContact=(ImageButton)rootView.findViewById(R.id.add_contact);
    namelist.clear();
    getContacts();

    addContact.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            addNewContact();
        }
    });
    return rootView;
}


public void getContacts() {
    ContentResolver cr = getActivity().getApplicationContext().getContentResolver();

    Cursor managedCursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            new String[]{ContactsContract.CommonDataKinds.Phone.CONTACT_ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.PHOTO_URI}, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE NOCASE ASC");


    while (managedCursor.moveToNext()) {

        String id = managedCursor.getString(0);
        String name = managedCursor.getString(1);
        String phoneNumber=managedCursor.getString(2);
        String image_uri=managedCursor.getString(3);
        email=getEmail(id);
        contact_id.add(id);
        namelist.add(name);
        number.add(phoneNumber);
        contact_image_uri.add(image_uri);
        contact_email.add(email);
    }
    managedCursor.close();

    ContactAdapter a = new ContactAdapter(getActivity(), contact_id, namelist, number,contact_image_uri,contact_email);
    contacts.setAdapter(a);
}


public void addNewContact()
{
    Intent in= new Intent(getContext(),AddContact.class);
    startActivity(in);
}

private String getEmail(String contactId) {

    String mailE=null;
    ContentResolver cr = getActivity().getApplicationContext().getContentResolver();
    Cursor cursor = cr.query(
            ContactsContract.CommonDataKinds.Email.CONTENT_URI,
            null,
            ContactsContract.CommonDataKinds.Email.CONTACT_ID +" = ?",
            new String[]{contactId}, null);

    while (cursor.moveToNext())
    {
        mailE=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
    }

    cursor.close();
    return mailE;
}

}