开发者

Using contact picker to get multiple data

I was able to successfully launch the contact picker and then set an editText in my activity to the phone number of the selected contact. What I'm trying to do now is to set two other editTexts to the first name and last name, respectively. I added the other editTexts and tried to retrieve the last and first name.

Here's what I have:

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 phon开发者_运维问答e 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");
    }
}

I must be missing something basic. Any thoughts?


Try this:

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


If you call a managedQuery it will return the contacts full name and the id but no phone number.

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));  
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜