4

Right now, I'm able to retrieve the phone number and set the text of my editText to that number. But when I try to get the last name or first name it doesn't work. Note the stuff I commented out.

Heres my code:

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class main extends Activity {

    private static final int CONTACT_PICKER_RESULT = 1001; 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button getContacts = (Button)findViewById(R.id.getContacts);
        getContacts.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(Intent.ACTION_PICK,
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
                startActivityForResult(i, CONTACT_PICKER_RESULT);

            }
        });
    }
    protected void onActivityResult(int reqCode, int resultCode, Intent data) {
        super.onActivityResult(reqCode, resultCode, data);
        if(resultCode == RESULT_OK) {
            switch (reqCode) {
            case CONTACT_PICKER_RESULT:
                Cursor cursor = null;
                String number = "";
                String lastName ="";
                try {

                    Uri result = data.getData();

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

                    //query
                    cursor = getContentResolver().query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone._ID + " = ? " , new String[] {id}, null);

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

                    int numberIdx = cursor.getColumnIndex(Phone.DATA);  

                    if(cursor.moveToFirst()) {
                        number = cursor.getString(numberIdx);
                        //lastName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
                    } else {
                        //WE FAILED
                    }
                } catch (Exception e) {
                    //failed
                } finally {
                    if (cursor!=null) {
                        cursor.close();
                    }
                    EditText numberEditText = (EditText)findViewById(R.id.number);
                    numberEditText.setText(number);
                    //EditText lastNameEditText = (EditText)findViewById(R.id.last_name);
                    //lastNameEditText.setText(lastName);

                }

            }
        }
LuxuryMode
  • 33,401
  • 34
  • 117
  • 188
  • What's the problem? What exactly doesn't work? – Michael Apr 22 '11 at 06:44
  • For anyone who wants first and last name separately, see: http://stackoverflow.com/questions/4301064/how-to-get-the-firstname-and-lastname-from-android-contacts Krzysztof's answer was particularly useful to me. – Ionoclast Brigham Oct 27 '11 at 18:45

2 Answers2

6

This is how i got the display name...

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
        case CONTACT_PICKER_RESULT:
            Cursor cursor = null;

            try {
                Uri result = data.getData();
                String id = result.getLastPathSegment();

                //Get Name
                cursor = getContentResolver().query(result, null, null, null, null);
                if (cursor.moveToFirst()) {
                    name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                } catch (Exception e) { }
        }
    }
}

Hope it helps :)

Tom O
  • 1,780
  • 2
  • 20
  • 43
  • Thanks! and how can I get the last name? or first name? – LuxuryMode Apr 22 '11 at 13:20
  • This gets both first and last name – Tom O Apr 22 '11 at 13:27
  • But I need the first name and last name separately. ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME and ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME should work, but that's what I'm having trouble with. – LuxuryMode Apr 22 '11 at 14:04
  • Split it by a space ' ' then get splitString[0] as first name and splitString[1] as last name? – Tom O Apr 22 '11 at 16:49
  • This helped me: http://stackoverflow.com/questions/1721279/how-to-read-contacts-on-android-2-0/1780818#1780818 – Sileria Oct 18 '12 at 04:26
1

If you have your contact id, you can use this method for retrieving all the other contact data:

Map<String, String> result = new HashMap<>();
Cursor cursor = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, ContactsContract.Data.CONTACT_ID + "='" + YOUR_CONTACT_ID + "'", null, null);
if (cursor != null) {
    while (cursor.moveToNext()) {
        String mime = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.MIMETYPE));
        switch (mime) {
            case ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE:
                result.put(FIRST_NAME, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME)));
                result.put(LAST_NAME, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME)));
                break;
            case ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE:
                result.put(CITY, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY)));
                result.put(STREET, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET)));
                result.put(ZIP, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE)));
                break;
            case ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE:
                if (ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE == cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE))) {
                    result.put(MOBILE, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
                }
                break;
        }
    }
    cursor.close();
}
return result;
artkoenig
  • 7,117
  • 2
  • 40
  • 61