Android 联系人选择器查询仅返回 phone 号码
Android contact picker query only returning phone number
我正在尝试从联系人选择器中提取一些数据,但我似乎只能得到 phone 号码。我尝试了多种不同的方法,而且我仍在学习 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));
}
}
你可以试试:
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));
然后告诉我它是否有效。
我正在尝试从联系人选择器中提取一些数据,但我似乎只能得到 phone 号码。我尝试了多种不同的方法,而且我仍在学习 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));
}
}
你可以试试:
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));
然后告诉我它是否有效。