问题描述
我正在尝试使用活动的内容解析器检索联系人的地址。任何地方都没有地址字段。我尝试了ContactsContract.CommonDataKinds.StructuredPostal.STREET,但它也给了我不同的价值。请查看该字段在我尝试检索的android中的外观的屏幕截图。
在下面的代码中,我正在遍历CONTENT_URI中“ John Doe”的所有值,但是此地址“ Slaughter Lane”从不显示,即我在Timber日志命令“ Found the address”的断点从不出现命中。
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
}
解决方法
您发布的代码未显示您如何尝试访问awk ' ##Starting awk program from here.
FNR==1{ ##Checking condition if this is first line then do following.
print ##Printing current line here.
next ##next will skip all further statements from here.
}
val{ ##Checking condition if val is NOT NULL then do following.
sub(/;$/,OFS val"&") ##Substituting last semi colon with OFS val and semi colon here.
print ##printing current line here.
val="" ##Nullify val here.
next ##next will skip all further statements from here.
}
{
val=$0 ##Setting current line value to val here.
}
END{ ##Starting END block of this program from here.
if(val!=""){ ##Checking condition if val is NOT NULL then do following.
print val ##Printing val here.
}
}' Input_file ##Mentioning Input_file name here.
,但我想我知道您是如何尝试的。
在StructuredPostal.STREET
上进行查询时,您只会收到一般的联系信息(例如Data.CONTACT_ID和Data.DISPLAY_NAME)以及电话专用列(例如Phone.NUMBER),因为每一行的模仿类型均为{ {1}}。
您无法从该查询中获取地址信息。
如果您需要多种类型的信息(例如电话和地址),则需要查询更通用的Phone.CONTENT_URI
uri,然后使用Phone.CONTENT_ITEM_TYPE
来确定此行的类型,电话,地址或其他内容...您可以通过在Data.CONTENT_URI
上添加选择来限制收到的行的类型。
下面是示例代码(在Java中):
Data.MIMETYPE
您可能为同一个联系人获得多个电话和地址,因此如果每个联系人都需要它,则应将Data.MIMETYPE
中的HashMap添加到所需数据中。