0

I am trying to access the contact name number and email ID using this code :

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;

public class GetAllDatas extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    readContacts();
}   

private void readContacts() {
    // TODO Auto-generated method stub

    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));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                System.out.println("name : " + name + ", ID : " + id);

                    // get the <span id="IL_AD1" class="IL_AD">phone number</span>


                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                                       ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                                       new String[]{id}, null);
                while (pCur.moveToNext()) {
                      String phone = pCur.getString(
                             pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                      System.out.println("phone" + phone);
                }
                pCur.close();

                // get email and type


                Cursor emailCur = cr.query(
                         ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                         null,
                         ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                         new String[]{id}, null);
                 while (emailCur.moveToNext()) {
                     // This would allow you get several email addresses
                         // if the email addresses were stored in an array
                     String email = emailCur.getString(
                                   emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                     String emailType = emailCur.getString(
                                   emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));

                   System.out.println("Email " + email + " Email Type : " + emailType);
                 }
                 emailCur.close();
            }
        }
    }

}
    }

When I run this code nothing would be displayed in my activity.

I need to access Contact name, number and email ID from my entire contact list on the activity window.

pb2q
  • 58,613
  • 19
  • 146
  • 147
BigBoss
  • 85
  • 1
  • 4
  • 13
  • possible duplicate of [How to Get Contact name, number and emai ID from a list of contacts?](http://stackoverflow.com/questions/10977887/how-to-get-contact-name-number-and-emai-id-from-a-list-of-contacts) – Niranj Patel Jun 12 '12 at 08:54
  • Where are you trying to display the contact information? The code above only reads from the database and there is no widget(like textview) used to display the received information. – Arun George Jun 12 '12 at 08:57
  • Actually I need to display the retrieved information as a list, I am a beginner to Android, so please help me to do the display as a list – BigBoss Jun 12 '12 at 09:13

1 Answers1

7

Check with this

   package stack.examples;

import java.util.ArrayList;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class StackOverFlowGetContactsActivity extends Activity {

    ListView lvItem;
    private Button btnAdd;
    String displayName="", emailAddress="", phoneNumber="";
    ArrayList<String> contactlist=new ArrayList<String>();
    ArrayAdapter<String> itemAdapter;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       lvItem = (ListView)this.findViewById(R.id.listView_items);  
       btnAdd = (Button)this.findViewById(R.id.btnAddItem);
       itemAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,contactlist);
       lvItem.setAdapter(itemAdapter);
       btnAdd.setOnClickListener(new View.OnClickListener() {
           public void onClick(View v) {
               readContacts();
           }
       });
    }

    private void readContacts()
    {
        ContentResolver cr =getContentResolver();
        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        while (cursor.moveToNext()) 
        {
            displayName="";emailAddress=""; phoneNumber="";
            displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));       
            String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            Cursor emails = cr.query(Email.CONTENT_URI,null,Email.CONTACT_ID + " = " + id, null, null);
            while (emails.moveToNext()) 
            { 
                emailAddress = emails.getString(emails.getColumnIndex(Email.DATA));
                break;
            }
            emails.close();
            if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
            {
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id}, null);
                while (pCur.moveToNext()) 
                {
                     phoneNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    break;
                }
                pCur.close();
            }
                contactlist.add("DisplayName: "+displayName+", PhoneNumber: "+phoneNumber+", EmailAddress: "+ emailAddress+"\n");
                itemAdapter.notifyDataSetChanged();
        }
        cursor.close(); 
    }
}

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listView_items"
        android:layout_width="match_parent"
        android:layout_height="288dp"
        android:layout_weight="0.03" >

    </ListView>

    <Button
        android:id="@+id/btnAddItem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.01"
        android:text="@string/add" />

</LinearLayout>
Ponmalar
  • 6,871
  • 10
  • 50
  • 80
  • now i am using your code...there are two errors like 1. Cursor.Close(); -- syntax error ,identifier expected after this token 2. public class Contact -- public type Contact must be defined in its own file any solutiion ? – BigBoss Jun 12 '12 at 09:39
  • Thanks for editing ,the code have no error it's working but nothing will be displayed in main activity. Will I add any Widget to xml file ? – BigBoss Jun 12 '12 at 10:02
  • you can disply this with listview or any Widget suitable for this – Ponmalar Jun 12 '12 at 10:14
  • No man I was searching for the past 1 hour. I know how to use the list view by using the string [],on this we store the values,then by extending activity to ListActivity but it don't work in this problem we need a dynamic list view ? Do u know how to do ? – BigBoss Jun 12 '12 at 11:57
  • Ponmalar Code is working properly got the desired output also.You did a great job to me thanks.thanks a lot. – BigBoss Jun 13 '12 at 06:38
  • I got the correct answer but little more editing is required,if you are not free let me put the code in to a new question ? – BigBoss Jun 13 '12 at 11:42
  • ponmalar I will ask the question . – BigBoss Jun 14 '12 at 08:03
  • See the edited code, clear the values displayName="";emailAddress=""; phoneNumber=""; for every time before adding new. – Ponmalar Jun 15 '12 at 10:08