Pre Exam Test Practice (SSC | Banking | IAS | GATE | State Level | CLAT | Railways | Insurance | Teaching Exams | NDA | Defence | IES |State Exam )

Organizational Research By

Surprising Reserch Topic

Question:Can't get the email address from ContactsContract

I'm trying to get the email address from the ContactsContract, but I only get an empty string! Name and number is working fine, but not the email address!

I confused and have been trying to solve this for days now, but I don't know what's wrong, if I have missed something in the code or what and I don't know how to solve this.

This part of the application has a listview of all names from the ContactsContract. When selecting a name in the listview, the code should get the name, number and email adress from the ContactsContract.

I would preciate some help to be able to continue the work! Perhaps there is a better way to solve this, then please tell me how to change! Thanks!

Since toast message dosen't show up inside the while loop, while(cursorEmail.moveToFirst()){.... I guess there is something wrong with the emails cursor!? It seems like it's empty!?

public class Activity_3 extends Activity {

ListView listView;

protected void onCreate(Bundle savedInstanceState) {

    listView = (ListView) findViewById(;

    String[] projection = { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone._ID };

    Cursor cursor1 = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, null, null, null);

    // From column
    String[] fromColumn = { ContactsContract.Contacts.DISPLAY_NAME };
    // To view
    int[] toView = { };


    ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.activity_3, cursor1, fromColumn, toView);


    listView.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView adapter, View view, int position, long id) {

            String[] projection = { ContactsContract.CommonDataKinds.Phone._ID,

            Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, null, null, null);


            String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String contactNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            String pos = Integer.toString(position);

            String contactEmailAddress = "?";   
            Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=" + contactId, null,  null);

                contactEmailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                Toast.makeText(Activity_3.this, contactEmailAddress, Toast.LENGTH_SHORT).show();

            Toast.makeText(Activity_3.this, pos + " " + contactId + " " + contactName + " " + contactNumber + " " + contactEmailAddress, Toast.LENGTH_SHORT).show();


asked Sep 13, 2013 in Java Interview Questions by rajesh
edited Sep 12, 2013
0 votes

Related Hot Questions

Government Jobs Opening