Java 导入联系人仅导入联系人列表中的第一个联系人号码
这个代码看起来不错。我在很多网站上做过研究,但当我运行代码时,只有联系人列表中的第一个号码出现在文本视图中。但是我想在编辑文本视图中显示所需的联系人号码Java 导入联系人仅导入联系人列表中的第一个联系人号码,java,android,contact,Java,Android,Contact,这个代码看起来不错。我在很多网站上做过研究,但当我运行代码时,只有联系人列表中的第一个号码出现在文本视图中。但是我想在编辑文本视图中显示所需的联系人号码 private void importContact() { Intent importContactIntent = new Intent(Intent.ACTION_PICK); importContactIntent.setType(ContactsContract.Contacts.CONTENT_TYPE);
private void importContact() {
Intent importContactIntent = new Intent(Intent.ACTION_PICK);
importContactIntent.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(importContactIntent, PICK_CONTACT);
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK) {
//Uri contactData = data.getData();
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
null, null);
cursor.moveToNext();
String name = cursor
.getString(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
destinationPhoneNumber = cursor
.getString(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//
enterPhoneNumber.setText(destinationPhoneNumber);
Toast.makeText(this,
name + " has number " + destinationPhoneNumber,
Toast.LENGTH_LONG).show();
cursor.close();
}
}
}
您可以这样查询以获取特定联系人的所有号码:
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
image_uri = cur
.getString(cur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
if (Integer
.parseInt(cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
//System.out.println("name : " + name + ", ID : " + id);
// NOW query all numbers of that particulat contact using contact_Id
Cursor pCur = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id }, null);
while (pCur.moveToNext()) {
// you can store phone in a arrayList
phone = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//System.out.println("phone" + phone);
}
pCur.close();
}
所以你想要一个特定联系人的所有号码?你觉得ans有用吗?@MohammedAli如果有多个号码,让用户选择。我只想找一个号码…你有什么问题?我向你展示了一种获取特定联系人所有号码的方法…@MohammedAli我能在这个话题上获得更多帮助吗。。。u answerCursor pCur=cr.query的注释中给出了问题,这一行显示了错误。(cr无法解决)ok无错误,但当我运行此应用程序并选择联系人时,会导致错误。和应用程序崩溃。单击某些联系人后,请查看代码中的错误。。。上面的代码已经过测试。log cat中的错误是:“未能传递结果ResultInfo{who=null,request=1,result=-1,data=Intent{dat=content://com.android.contacts/contacts/lookup…}}“Contacts contract.Contacts.HAS_PHONE_NUMBER为我返回-1,有什么建议吗?