仅当他们有电话号码时,如何获取所有联系人的全名和电话号码?
我试图获取所有有电话号码的联系人,并记录他们的全名和电话号码(以及将来的联系人照片),但我陷入困境。这是我的代码:
String contacts = "";
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts._ID));
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone == "1") {
contacts += cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)) + ":" + "how to get number?" + "|";
}
}
cursor.close();
如果联系人有电话号码,则字符串 hasPhone 应包含“1”,然后将该姓名和该人的电话号码添加到“联系人”字符串中。即使 hasPhone 确实包含“1”,(从 logcat 检查)条件语句中没有代码运行。另外,如何获取电话号码,ContactsContract.Contacts 中没有任何号码。
I am trying to get all contacts that have a phone number, and record their full name and phone number, (and in the future, their contact photo), but I am stuck. here is my code:
String contacts = "";
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts._ID));
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone == "1") {
contacts += cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)) + ":" + "how to get number?" + "|";
}
}
cursor.close();
String hasPhone should contain "1" if the contact has a phone number, then add that name and the persons phone number to the "contact" string. Even though hasPhone does contain "1", (checked from logcat) no code in the condition statement runs. Also, how do you get the phone number, there is nothing in ContactsContract.Contacts for number.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
Try this:
改为:
== 运算符检查对象相等性,也就是说,如果 hasPhone 与“1”是同一个对象,则显然是 false。
您想要检查字典序是否相等,因此您应该使用 String 的 equals 方法,该方法比较两个对象的字符串相等性,即检查两者是否具有相同的字符顺序。
此外,请考虑使用 LookupKey,如下所述: http://developer.android.com/ resources/articles/contacts.html
如果您想保存特定联系人的未来参考。
Change to:
== operator check for object equality, that is to say, if hasPhone is the same Object as "1" which is clearly false.
You want to check for Lexicographic equality, so you should use String's equals method, which compare both Objects string equality, meaning, checks if both have the same order of characters.
Moreover, consider using the LookupKey, as described here: http://developer.android.com/resources/articles/contacts.html
If you want to save future reference for specific contact.