如何从联系人列表中读取联系人姓名和号码

问题描述:

我正在尝试从用户联系人列表中读取联系人号码。这里是我的代码如何从联系人列表中读取联系人姓名和号码

Cursor cursor = getContacts(); 
if(cursor.getCount()>0){ 
    while (cursor.moveToNext()) { 
     String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)); 
     int numberField = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); 
     textViewDisplay.append("Name: "); 
     textViewDisplay.append(displayName+"Number :"+numberField); 
     textViewDisplay.append("\n"); 
    } 
} 
+0

你面临什么问题? – 2013-03-05 12:17:35

+0

是的,我收到联系人的名字,但没有得到联系号码,它返回-1 – Supreet 2013-03-05 12:18:40

您正在cursor.getColumnIndex(COLUMN)int。因此,它所说的方法返回发送给它的索引COLUMN作为参数。你需要它永远不会被一个int包含它的大小总是大于4个字节的电话​​号码,并且还包含一些特殊字符,如+

所以,你需要把你的Number一些String可变

使用String numberField = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

如Anuj所建议的。

使用本

String numberField = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 

代替

int numberField = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); 
+0

在获取联系人时发生非法状态异常。 – Supreet 2013-03-05 12:36:32

+0

使用这个URI'乌里phoneUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;' – 2013-03-05 12:43:30

+0

和名称使用'cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME));' – 2013-03-05 12:45:00

它返回 “1” 指的是联系人有电话号码。试试这个,

String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
         String cNumber=""; 
         if (hasPhone.equalsIgnoreCase("1")) 
         { 
          Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
          ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null); 
          phones.moveToFirst(); 
          cNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 

          phones.close(); 

         } 

试试这个:

public void readContacts() { 
      ContentResolver cr = getContentResolver(); 
      Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,null, null, null); 
      if (cur.getCount() > 0) { 
       while (cur.moveToNext()) { 
        if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { 

         // Get contact id (id) 
         String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)); 

         // Get contact name (displayName) 
         String displayName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 


         // Get Phone Number.... 

         Uri URI_PHONE = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; 
         String SELECTION_PHONE = ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = ?"; 
         String[] SELECTION_ARRAY_PHONE = new String[] { id }; 

         Cursor currPhone = cr.query(URI_PHONE, null,SELECTION_PHONE, SELECTION_ARRAY_PHONE, null); 
         int indexPhoneNo = currPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); 
         int indexPhoneType = currPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE); 

         if (currPhone.getCount() > 0) { 

          while (currPhone.moveToNext()) { 
           String phoneNoStr = currPhone.getString(indexPhoneNo); 
           String phoneTypeStr = currPhone.getString(indexPhoneType); 
          } 
         } 
         currPhone.close(); 
        } 
       } 
      } 
      cur.close(); 
     } 
+0

它抛出CursorIndexOutofBoundException – Supreet 2013-03-05 12:53:01

+0

OK ,按照这个http://samir-mangroliya.blogspot.in/p/android-read-contact-and-display-in.html,这里是你的好例子 – Anand 2013-03-05 13:44:29