I'm trying to retrieve the address of a contact using the activity's content resolver. There is no Address field anywhere. I tried ContactsContract.CommonDataKinds.StructuredPostal.STREET but it also gives me a different value. Please see the screen shot of how the field looks like in android which I'm trying to retrieve.
In my code below, I'm iterating through all values in the CONTENT_URI for 'John Doe', but this address, "Slaughter Lane" never shows up i.e. my breakpoint at the Timber log command 'Found the address' is never hit.
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
}
