How to put a system contact in EditText?

0

I'm creating an application where the user will click on a EditText which will take you to the system contact list, where it will click and the name of the contact will appear in EditText . However, it is precisely to get this contact and fill in the EditText that I am caught. My code is this:

Comvoce3 = (EditText) findViewById(R.id.Comvoce);

    Comvoce3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intentList = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
            startActivityForResult(intentList, RESULT_PICK);
            Comvoce3.setHint("");
        }

    });
    
asked by anonymous 20.07.2017 / 23:20

1 answer

0

Try to implement the result of Activity (since it is using startActivityForResult ):

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == RESULT_PICK) {
        if (resultCode == RESULT_OK) {
            Uri contactUri = data.getData();
            String[] projection = {ContactsContract.Contacts.DISPLAY_NAME};
            Cursor cursor = getContentResolver()
                    .query(contactUri, projection, null, null, null);
            cursor.moveToFirst();
            int column = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
            String contact = cursor.getString(column);

            Comvoce3.setHint(contact);
        }
    }
}
    
21.07.2017 / 15:08