我有一个“联系人”类,我正在使用适用于 Android 的标准联系人选择器代码,但我也想获取联系人的“id”和“姓名”,而不仅仅是号码......这是我的代码。
@Override public void onActivityResult( int requestCode, int resultCode, Intent intent ) {
super.onActivityResult( requestCode, resultCode, intent );
if ( requestCode == PICK_CONTACT_REQUEST ) {
if ( resultCode == RESULT_OK ) {
Uri contactUri = intent.getData();
String[] projection = {Phone.NUMBER};
Cursor cursor = getContentResolver()
.query(contactUri, projection, null, null, null);
cursor.moveToFirst();
int column = cursor.getColumnIndex(Phone.NUMBER);
String number = cursor.getString(column);
int contactNumber = Integer.parseInt(number);
//i need to get the contactId and the contactName here
Contact newContact = new Contact(contactId,contactName,contactNumber);
EditText _contactName = (EditText) findViewById(R.id.contact_name);
_contactName.setText(number.toString());
}
}
}
我找到了解决方案。如果有人需要,就在这里。
Uri contactUri = intent.getData();
String[] projection = {Phone.NUMBER, Phone.DISPLAY_NAME, Phone._ID};
Cursor cursor = getContentResolver()
.query(contactUri, projection, null, null, null);
cursor.moveToFirst();
int columnNumber = cursor.getColumnIndex(Phone.NUMBER);
String contactNumber = cursor.getString(columnNumber);
int columnName = cursor.getColumnIndex(Phone.DISPLAY_NAME);
String contactName = cursor.getString(columnName);
int columnID = cursor.getColumnIndex(Phone._ID);
String ID = cursor.getString(columnID);
int contactID = Integer.parseInt(ID);