在android应用程序中从联系人列表中仅获取一个联系人
从我的联系人列表中选择一个联系人后,它不会只得到一个联系人,而是所有联系人 以下是我为获得电话号码所做的工作:在android应用程序中从联系人列表中仅获取一个联系人,android,Android,从我的联系人列表中选择一个联系人后,它不会只得到一个联系人,而是所有联系人 以下是我为获得电话号码所做的工作: protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { switch (requestCode) { case CONTACT_PICKER_RESULT:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case CONTACT_PICKER_RESULT:
ContentResolver cr = getContentResolver();
Cursor c = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (c.moveToFirst()) {
String id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
Cursor phone = cr.query(Phone.CONTENT_URI,null,null,null,null);
while(phone.moveToNext()){
String number = phone.getString(phone.getColumnIndex(Phone.NUMBER));
int type = phone.getInt(phone.getColumnIndex(Phone.TYPE));
this.numeros.add(number);
}
}
break;
}
this.mostarToast(this.numeros.size() + " contatos adicionados.");
} else {
// gracefully handle failure
Log.w("Erro", "Erro ao adicionar contato!");
}
}
“mostrarToast”方法只是用来显示我选择了多少联系人。但它显示的是101个联系人,而不是一个。我怎样才能准确地选一个呢?我想你选的是所有的数字,而不是一个:
Cursor phone = cr.query(Phone.CONTENT_URI,null,ContactsContract.Contacts._ID+" = ?" ,new String[]{id},null);
这将从该唯一联系人id获取所有号码,请尝试某些联系人可能没有电话号码,您可以尝试使用另一个。