获取忽略口音的联系人
Fetch contacts ignoring accents
我从 phone 中获取联系人,如下所示。我想忽略重音,例如名称 "Jérome" 将是 return 用于搜索 "jero" 或 "jéro".
var contacts = listOf<Contact>()
val displayNameProjection = arrayOf(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI, ContactsContract.Contacts.DISPLAY_NAME_PRIMARY)
val whereName = ContactsContract.Contacts.DISPLAY_NAME_PRIMARY + " LIKE ?"
val whereNameParams = arrayOf( "%" + search + "%")
val contactCursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI,
displayNameProjection, whereName, whereNameParams,ContactsContract.Contacts.DISPLAY_NAME_PRIMARY)
contactCursor?.let { cursor ->
contacts = generateSequence { if (cursor.moveToNext()) cursor else null }
.take(10)
.map {
Contact( it.getString(it.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY)),
it.getString(it.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI)))
}
.toList()
cursor.close()
}
使用 CONTENT_FILTER_URI
API 按姓名快速搜索联系人,这应该会自动为您处理重音符号:
String name = "jero";
Uri searchUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(name));
Cursor cur = getContentResolver().query(searchUri, null, null, null, null);
DatabaseUtils.dumpCursor(cur);
if (cur != null) cur.close();
我从 phone 中获取联系人,如下所示。我想忽略重音,例如名称 "Jérome" 将是 return 用于搜索 "jero" 或 "jéro".
var contacts = listOf<Contact>()
val displayNameProjection = arrayOf(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI, ContactsContract.Contacts.DISPLAY_NAME_PRIMARY)
val whereName = ContactsContract.Contacts.DISPLAY_NAME_PRIMARY + " LIKE ?"
val whereNameParams = arrayOf( "%" + search + "%")
val contactCursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI,
displayNameProjection, whereName, whereNameParams,ContactsContract.Contacts.DISPLAY_NAME_PRIMARY)
contactCursor?.let { cursor ->
contacts = generateSequence { if (cursor.moveToNext()) cursor else null }
.take(10)
.map {
Contact( it.getString(it.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY)),
it.getString(it.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI)))
}
.toList()
cursor.close()
}
使用 CONTENT_FILTER_URI
API 按姓名快速搜索联系人,这应该会自动为您处理重音符号:
String name = "jero";
Uri searchUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(name));
Cursor cur = getContentResolver().query(searchUri, null, null, null, null);
DatabaseUtils.dumpCursor(cur);
if (cur != null) cur.close();