tags:

views:

64

answers:

1

I want to read the phone number and name using Android. I used the following code. It can only read phone name. How can I read phone number. I am very new in Android. Could any one hlep plz.

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)
    {
          Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);    
        while (pCur.moveToNext()) 
            {
               String lnumber = cur.getString( cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            } 
        pCur.close();
    }                  
    }

Thanks in advance.

+1  A: 

Sorry I mistake something in code. Its woring now. The code would be :

while (pCur.moveToNext())
{
String lname = pCur.getString( pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
} }

Ashraf