0

I'm trying to extract some data from the contact picker, but it seems that all I'm able to get is the phone number. I tried multiple different things, and as I'm still learning Java, I would really appreciate some help.

What I want:

what I get:

public void onCreate(Bundle savedInstanceSate) {
    (findViewById(R.id.button1)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
            startActivityForResult(i, PICK_CONTACT);
        }
    });
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PICK_CONTACT && resultCode == RESULT_OK) {

        //phonedata
        Uri contactUri = data.getData();
        Cursor cursor = getContentResolver().query(contactUri, null, null, null, null);

        //phone
        cursor.moveToFirst();
        int column = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
        TextView Textphone = (TextView)findViewById(R.id.Textphone);
        Textphone.setText(cursor.getString(column));

        //otherdata
        Uri contactData = data.getData();
        Cursor c =  getContentResolver().query(contactData, null, null, null, null);

        //name
        c.moveToFirst();
        int name = c.getColumnIndex(StructuredName.DISPLAY_NAME);
        TextView Textname = (TextView)findViewById(R.id.Textname);
        Textname.setText(c.getString(name));

        //email
        c.moveToFirst();
        int email = c.getColumnIndex(Email.DATA);
        TextView TextEmail = (TextView)findViewById(R.id.Textemail);
        TextEmail.setText(c.getString(email));

        //adress
        c.moveToFirst();
        int address = c.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS);
        TextView Textaddress = (TextView)findViewById(R.id.Textaddress);
        Textaddress.setText(c.getString(address));

        //Nickname
        c.moveToFirst();
        int nickname = c.getColumnIndex(Nickname.NAME);
        TextView Textnickname = (TextView)findViewById(R.id.Textnickname);
        Textnickname.setText(c.getString(nickname));
     }
}
gustavohenke
  • 40,997
  • 14
  • 121
  • 129
Doommius
  • 3
  • 1

1 Answers1

0

You can try :

String name=c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
TextView Textname = (TextView)findViewById(R.id.Textname);
Textname.setText(c.getString(name));

instead of:

    int name = c.getColumnIndex(StructuredName.DISPLAY_NAME);
    TextView Textname = (TextView)findViewById(R.id.Textname);
    Textname.setText(c.getString(name)); 

and tell me if it worked.

chetna
  • 94
  • 5