获取联系人姓名? [英] Get contact name?
本文介绍了获取联系人姓名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要得到的联系人姓名,但我不能。看<一后href=\"http://stackoverflow.com/questions/4301064/how-to-get-the-first-name-and-last-name-from-android-contacts\">this回答,我就先用家人的名字,因为和显示,但是毫无效果
@覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
如果(要求code == PICK_CONTACT&放大器;&安培;结果code == RESULT_OK){
乌里contactUri = data.getData();
。光标光标= getContentResolver()查询(contactUri,NULL,NULL,NULL,NULL);
cursor.moveToFirst(); //移动到第一排......其实我不知道为什么这部分是必要的,但我得到一个错误,没有它...
INT NumberColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); //诠释列是数字的列
INT名称列= cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
字符串contactNumber = cursor.getString(NumberColumn);
字符串CONTACTNAME = cursor.getString(名称列);
Toast.makeText(MainActivity.this,+ contactNumber ++联系人姓名,Toast.LENGTH_SHORT).show(); }
/
公共无效的addContact(视图v){//监听的OnClick推出联系人选择器
意向意图=新意图(Intent.ACTION_PICK,ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(意向,PICK_CONTACT);
}
解决方案
尝试下面code为获得特定的号码联系
@覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
super.onActivityResult(要求code,结果code,数据); 如果(要求code == REQUEST_ code_PICK_CONTACTS&放大器;&安培;结果code == RESULT_OK){
Log.d(TAG,回应:+ data.toString());
uriContact = data.getData(); retrieveContactName(); }
} 私人无效retrieveContactName(){ 字符串CONTACTNAME = NULL; //查询联系人数据存储
。光标光标= getContentResolver()查询(uriContact,NULL,NULL,NULL,NULL); 如果(cursor.moveToFirst()){ // DISPLAY_NAME =联系人的显示名称。
// HAS_PHONE_NUMBER =的这种接触是否具有至少一个电话号码的指示器。 CONTACTNAME = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
} cursor.close(); Log.d(TAG,联系人姓名:+联系人姓名); }
更多详细信息请参考以下链接的https://tausiq.word$p$pss.com/2012/08/23/android-get-contact-details-id-name-phone-photo/
I want to get the contact name, but I'm not able to. After looking at this answer, I tried to get the name using family, given, and display, but nothing worked
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_CONTACT && resultCode == RESULT_OK) {
Uri contactUri = data.getData();
Cursor cursor = getContentResolver().query(contactUri, null, null, null, null);
cursor.moveToFirst(); //Move to first row...I actually dont know why this part is necessary, but I get an error without it...
int NumberColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); //Int column is the column of the numbers
int NameColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
String contactNumber = cursor.getString(NumberColumn);
String contactName = cursor.getString(NameColumn);
Toast.makeText(MainActivity.this, ""+ contactNumber +"" +contactName, Toast.LENGTH_SHORT).show();
}
/
public void addContact(View v){ //OnClick listener to launch contact picker
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
解决方案
Try below code for getting contact of specific number
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_PICK_CONTACTS && resultCode == RESULT_OK) {
Log.d(TAG, "Response: " + data.toString());
uriContact = data.getData();
retrieveContactName();
}
}
private void retrieveContactName() {
String contactName = null;
// querying contact data store
Cursor cursor = getContentResolver().query(uriContact, null, null, null, null);
if (cursor.moveToFirst()) {
// DISPLAY_NAME = The display name for the contact.
// HAS_PHONE_NUMBER = An indicator of whether this contact has at least one phone number.
contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
}
cursor.close();
Log.d(TAG, "Contact Name: " + contactName);
}
More detail refer below link https://tausiq.wordpress.com/2012/08/23/android-get-contact-details-id-name-phone-photo/
这篇关于获取联系人姓名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文