[英]Android SDK-Get phone number of a specific contact
How can I get the phone number of a specific contact by it's id? 如何通过ID获取特定联系人的电话号码?
So far I have this 到目前为止,我有这个
while (cursor.moveToNext()) {
String displayName = cursor.getString(cursor
.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
String contactId = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts._ID));
if (Integer.parseInt(cursor.getString(
cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor phones = managedQuery( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{contactId}, null);
while (phones.moveToNext()) {
phoneNumber = phones.getString(
phones.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
}
//phones.close();
}
else
phoneNumber = "No number";
tv.append(displayName);
tv.append(" ");
tv.append(phoneNumber);
tv.append("\n");
}
cursor.close();
}
And while I get the correct display names, for each one of them I get the phone number of the first contact in the catalogue. 当我获得正确的显示名称时,对于其中的每个显示名称,我都会获得目录中第一个联系人的电话号码。 Any ideas?
有任何想法吗?
如果只需要显示名称和电话号码,则只需查询包含DISPLAY_NAME_PRIMARY列的Phones.CONTENT_URI。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.