Android获取联系人姓名保持返回-1

时间:2013-07-11 19:25:55

标签: android android-contacts

我有一个发送到Contacts Activity的Intent,然后Main Activity在此Callback中获得结果。问题是列总是-1。这就是说如何获得联系人姓名?

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PICK_CONTACT_REQUEST) {
        if (resultCode == RESULT_OK) {
            Uri contactUri = data.getData();
            String[] projection = {Phone.NUMBER};
            Cursor cursor = getContentResolver().query(contactUri, 
                        projection, null, null, null);
            if (cursor.moveToFirst()) {
            int column = cursor.getColumnIndex(
                ContactsContract.PhoneLookup.DISPLAY_NAME);
            if (column >= 0) {
                String name = cursor.getString(column);
                addString(name);
            } else {
                addString("Could Not Get User Name");
                }
            }
        }
    }

}

1 个答案:

答案 0 :(得分:1)

您的预测为String[] projection = {Phone.NUMBER};,因此光标将只包含此列名称,您需要使用String[] projection = {Phone.NUMBER, ContactsContract.PhoneLookup.DISPLAY_NAME};才能访问Display_Name。

还要确保ContentProvider返回所需的信息。