2013-03-05 107 views
0

我正在嘗試從用戶聯繫人列表中讀取聯繫人號碼。這裏是我的代碼如何從聯繫人列表中讀取聯繫人姓名和號碼

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

回答

2

您正在cursor.getColumnIndex(COLUMN)int。因此,它所說的方法返回發送給它的索引COLUMN作爲參數。你需要它永遠不會被一個int包含它的大小總是大於4個字節的電話​​號碼,並且還包含一些特殊字符,如+

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

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

如Anuj所建議的。

1

使用本

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

它返回 「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(); 

         } 
1

試試這個:

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

相關問題