-1

I want to get specific contact id and name. what is the best way to get it?

if (requestCode == mActions.REQUEST_PICK_CONTACT && resultCode == RESULT_OK) {
   Uri uriContact = (Uri)data.getData();
        String/int id = ???
        String name = ???
        String phoneNumber = ???
    }
  }

I just want to get the contact i choose data and not all the contact. how can i do it?

Anna
  • 499
  • 1
  • 6
  • 24

1 Answers1

1

This is how you can get Contact metadata if you've contact URI.

String id, name, phone, hasPhone;
int idx;
Cursor cursor = getContentResolver().query(contactUri, null, null, null,   null);
if (cursor.moveToFirst()) {
   idx = cursor.getColumnIndex(ContactsContract.Contacts._ID);
   id = cursor.getString(idx);

   idx = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
   name = cursor.getString(idx);

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

}

Noman Rafique
  • 3,735
  • 26
  • 29