0

From input box, I am throwing a dialog to pick up the contacts. My code looks like below

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toContacts = (EditText)findViewById(R.id.To);
        toContacts.setOnClickListener(new EditText.OnClickListener(){public void onClick(View v){pickContacts();}});

    }

    public void pickContacts(){
        Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
        startActivityForResult(intent, 1);
    }

    public void onActivityResult(int reqCode, int resultCode, Intent data){
        super.onActivityResult(reqCode, resultCode, data);
        String name = "";
          switch (reqCode) {
            case (1) :
              if (resultCode == Activity.RESULT_OK) {
                Uri contactData = data.getData();
                //@SuppressWarnings("deprecation")
                Cursor c = null;
                try{
                     //c =  managedQuery(contactData, null, null, null, null);
                     c = getContentResolver().query(contactData, null, Contacts.DISPLAY_NAME, null, null);
                     if (c.moveToFirst()) {
                          name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                          //String number = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                        }
                } catch(Exception e){
                    Log.e(DEBUG_TAG, "failed to get contacts data", e);
                } finally {
                    if(c != null){
                        c.close();
                    }
                    EditText toSendContact = (EditText) findViewById(R.id.To);
                    toSendContact.setText(name);
                }
              }
              break;
          }

    }

But it is not returning the selected result back in input box.

Charles
  • 50,943
  • 13
  • 104
  • 142
yogsma
  • 10,142
  • 31
  • 97
  • 154

1 Answers1

0

Replace

c = getContentResolver().query(contactData, null, Contacts.DISPLAY_NAME, null, null);

with

c = getContentResolver().query(contactData, new String[] { ContactsContract.Contacts.DISPLAY_NAME }, null, null, null);

You have used selection instead projection.

biegleux
  • 13,179
  • 11
  • 45
  • 52