-1

I have started the contact picker activity

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                    startActivityForResult(intent, enumRequestCode.PICK_CONTACT.ordinal());

Then in onActivityResult i have this code to get phone number (And display_name, but that is not good, enough... i need firstname and surname to be able to support different country inputs):

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if(enumRequestCode.PICK_CONTACT.ordinal() == requestCode){
              if (resultCode == Activity.RESULT_OK) {
                  Cursor cursor =  managedQuery(data.getData(), null, null, null, null);      
                   while (cursor.moveToNext()) 
                   {           
                       String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                       String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 

                       String phoneNumber = "";
                       int hasPhone = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));


                       String firstName = "";
                       String surName   = "";


                           if (hasPhone >= 1){
                                Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
                                phones.moveToNext();
                                phoneNumber  = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));



  phones.close();
                       }

I've tried with:

//  String firstName  = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
//  String surName    = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));

But that didn't work out very well.

I've seen this one, but it didn't really help me alot: How to get the first name and last name from Android contacts?

Anyone can help me with this one?

thanks in advance!

Community
  • 1
  • 1
Ikky
  • 2,826
  • 14
  • 47
  • 68

2 Answers2

0
cursor = getContentResolver().query(Data.CONTENT_URI,
                  new String[] {Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL},
                  Data.CONTACT_ID + "=?" + " AND "
                          + Data.MIMETYPE + "='" + StructuredName.CONTENT_ITEM_TYPE + "'",
                  new String[] {String.valueOf(id)}, null);

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

This is how I did it for first name and last name... and indeed other forms of name with the MIMETYPE = StructuredName.CONTENT_ITEM_TYPE.

Mark
  • 53
  • 6
0
Cursor cursor =  managedQuery(intent.getData(), null, null, null, null);      
        while (cursor.moveToNext()) {           
            String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 

            String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

            if (hasPhone.equalsIgnoreCase("1"))
                hasPhone = "true";
            else
                hasPhone = "false" ;

            if (Boolean.parseBoolean(hasPhone)) {
                Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
                if(phones.getCount() > 1) {
                    ArrayList<String> p = new ArrayList<String>();  

                    while (phones.moveToNext()) {
                        p.add(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
                    }
                    final CharSequence[] items = p.toArray(new CharSequence[p.size()]);
                    AlertDialog.Builder builder = new AlertDialog.Builder(newContact.this);
                    builder.setItems(items, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int item) {
                            phoneNumber = (String) items[item];
                            showContentSettings();
                        }
                    });
                    AlertDialog alert = builder.create();
                    alert.show();
                }else {
                    phones.moveToFirst();
                    phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    showContentSettings();
                }
                phones.close();
            }else {
                showDialog(DIALOG_PHONE_NOT_FOUND);
            }
        }
        cursor.close();
        Log.d(TAG,"name: " + name + "\nphoneNumber: " + phoneNumber);
alezhka
  • 738
  • 2
  • 12
  • 29
  • uhm, i've already gotten the phonenumber, but i need firstname and surname. It does not look like you have provided anything for that? – Ikky Nov 11 '11 at 10:18
  • parse vcard data vcard = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.CONTENT_VCARD_TYPE)); http://ru.wikipedia.org/wiki/VCard – alezhka Nov 11 '11 at 10:33