【发布时间】:2021-01-24 14:04:45
【问题描述】:
我正在尝试使用活动的内容解析器检索联系人的地址。任何地方都没有地址字段。我试过 ContactsContract.CommonDataKinds.StructuredPostal.STREET 但它也给了我不同的价值。请查看我正在尝试检索的 android 中该字段的屏幕截图。
在下面的代码中,我正在遍历 CONTENT_URI 中“John Doe”的所有值,但是这个地址“Slaughter Lane”从未出现,即我在 Timber 日志命令“找到地址”处的断点从未出现命中。
fun getContact(activity: FragmentActivity, name: String): Contact? {
var contact = Contact()
val cr : ContentResolver = activity.contentResolver
var selectionArgs: Array<String> = arrayOf("John Doe")
val cursor = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
"DISPLAY_NAME = ?",
selectionArgs,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC"
)
if (cursor != null) {
try {
while (cursor.moveToNext()) {
cursor.columnNames.forEach {
var stringValue: String? = cursor.getString(cursor.getColumnIndex(it))
if (stringValue!=null) {
if (stringValue.contains("Slaughter")){
Timber.i("Found the address");
}
}
}
}
} finally {
cursor.close()
}
}
return if (contact.name != null) contact else null
}
【问题讨论】:
-
请发布您的代码,您可能查询了错误的 CONTENT_URI
-
代码..当然!!忘了那个。现在添加,谢谢
标签: android kotlin android-contacts