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");
    }
}
4

4 回答 4

2

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

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

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

正如 Anuj 所建议的那样。

于 2013-03-05T12:27:37.830 回答
1

用这个

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

代替

int numberField = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
于 2013-03-05T12:24:24.363 回答
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();

                      }
于 2013-03-05T12:26:49.783 回答
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();
        }
于 2013-03-05T12:39:14.880 回答