使用联系人选择器获取多个数据

时间:2011-04-21 21:44:25

标签: android

我能够成功启动联系人选择器,然后在我的活动中将editText设置为所选联系人的电话号码。我现在要做的是分别将另外两个editTexts设置为名字和姓氏。我添加了其他editTexts并尝试检索姓氏和名字。

这就是我所拥有的:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
        case CONTACT_PICKER_RESULT:
            Cursor cursor = null;
            String number = "";
            String lastName = "";
            String firstName = "";
            try {
                Uri result = data.getData();
                Log.v(DEBUG_TAG, "Got a contact result: "
                        + result.toString());

                // get the contact id from the Uri
                String id = result.getLastPathSegment();

                // query for phone number
                cursor = getContentResolver().query(Phone.CONTENT_URI,
                        null, Phone.CONTACT_ID + "=?", new String[] { id },
                        null);

                int phoneIdx = cursor.getColumnIndex(Phone.DATA);
                int lastNameIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds
                        .StructuredName.FAMILY_NAME);
                int firstNameIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds
                        .StructuredName.GIVEN_NAME);

                // get the phone number
                if (cursor.moveToFirst()) {
                    number = cursor.getString(phoneIdx);
                    lastName = cursor.getString(lastNameIdx);
                    firstName = cursor.getString(firstNameIdx);


                    Log.v(DEBUG_TAG, "Got number " + number);
                } else {
                    Log.w(DEBUG_TAG, "No results");
                }
            } catch (Exception e) {
                Log.e(DEBUG_TAG, "Failed to get phone number data", e);
            } finally {
                if (cursor != null) {
                    cursor.close();
                }
                EditText phoneNumberEditText = (EditText) findViewById(R.id.number);
                phoneNumberEditText.setText(number);
                EditText lastNameEditText = (EditText)findViewById(R.id.last_name);
                lastNameEditText.setText(lastName);
                EditText firstNameEditText = (EditText)findViewById(R.id.first_name);
                firstNameEditText.setText(firstName);
                if (number.length() == 0) {
                    Toast.makeText(this, "No phone number found for this contact.",
                            Toast.LENGTH_LONG).show();
                }
                if(lastName.length()==0) {
                    Toast.makeText(this, "No last name found for this contact.", 
                            Toast.LENGTH_LONG).show();
                }
                if(firstName.length()==0) {
                    Toast.makeText(this, "No first name found for this contact.", 
                            Toast.LENGTH_LONG).show();
                }

            }

            break;
        }

    } else {
        Log.w(DEBUG_TAG, "Warning: activity result not ok");
    }
}

我必须遗漏一些基本的东西。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

试试这个:

String lastName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME))

答案 1 :(得分:0)

如果您调用managedQuery,它将返回联系人全名和ID但没有电话号码。

Cursor cursor =  managedQuery(intent.getData(), null, null, null, null);  
cursor.moveToNext(); String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));  
String  name=cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));