Android联系人选择器查询仅返回电话号码

时间:2015-06-29 10:26:44

标签: java android android-contacts contactscontract

我正在尝试从联系人选择器中提取一些数据,但似乎所有我能够获得的是电话号码。我尝试了多种不同的东西,因为我还在学习Java,我真的很感激一些帮助。

我想要的:

我得到了什么:

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));
     }
}

1 个答案:

答案 0 :(得分:0)

您可以尝试:

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

而不是:

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

并告诉我它是否有效。