无法使用ContactsContract.Contact.HAS_PHONE_NUMBER显示联系人号码

时间:2015-11-20 07:48:22

标签: android

我正在尝试从手机中获取联系人详细信息。只显示名称。这是我的代码。我怎样才能获得电话号码。

    ContentResolver cr = getContentResolver();
    Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, new  String[]{ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.HAS_PHONE_NUMBER}, null, null, null);

    String[] columns = new String[]{ContactsContract.Data.DISPLAY_NAME, ContactsContract.Contacts.HAS_PHONE_NUMBER};
    int[] to = new int[]{R.id.name, R.id.number};

    SimpleCursorAdapter madapter = new SimpleCursorAdapter(this, R.layout.item_activity, cursor, columns, to, 0);
    ListView listView = (ListView) findViewById(R.id.contactList);
    listView.setAdapter(madapter);

3 个答案:

答案 0 :(得分:2)

替换你的两行

Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, new  String[]{ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.HAS_PHONE_NUMBER}, null, null, null);
String[] columns = new String[]{ContactsContract.Data.DISPLAY_NAME, ContactsContract.Contacts.HAS_PHONE_NUMBER};

Cursor cursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new  String[]{ContactsContract.CommonDataKinds.Phone._ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER}, null, null, null);
String[] columns = new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER};

您需要使用ContactsContract.CommonDataKinds.Phone.NUMBER来获取电话号码。

答案 1 :(得分:0)

ContentResolver contactResolver = getContentResolver(); 
Cursor cursor = contactResolver.query(Phone.CONTENT_URI, null, Phone.NUMBER + "=?", new String[]{contactNumber}, null);

if(cursor.getCount() > 0){
    cursor.moveToFirst();
    do {
       String number = cursor.getString(mCursor.getColumnIndex(Phone.NUMBER));
    }while (cursor.moveToNext() ); 
}

答案 2 :(得分:0)

您可以使用 contactID

检索电话号码
// getting contacts ID
Cursor cursorID = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, new String[]{ContactsContract.Contacts._ID}, null, null, null);

if(cursorID.moveToFirst()) {
        contactID = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts._ID));
    }

cursorID.close();

// Using the contact ID now we will get contact phone number
Cursor cursorPhone = getContentResolver().query(
                              ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                              new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
                              ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " +
                              ContactsContract.CommonDataKinds.Phone.TYPE + " = " +
                              ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,
                              new String[]{contactID},
                              null
                      );

if (cursorPhone.moveToFirst()) {
     contactNumber = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}