0

I fetch contacts from the phone as below. I would like to ignore accent for exemple the name "Jérome" would be return either for search "jero" or "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()
        }
marmor
  • 25,207
  • 10
  • 99
  • 145
Nicolas
  • 633
  • 6
  • 15
  • There is no simply method to do this. See : [Query how to ignore accent](https://stackoverflow.com/questions/8400227/android-simpleadapter-filter-query-how-to-ignore-accents#answer-8400407) – Kévin Giacomino Jan 25 '19 at 09:09
  • If the accent is only on the contacts and not in the SQL database you can parse the search string before Querying, is that your goal? – Legion Jan 25 '19 at 09:15
  • Possible duplicate of: https://stackoverflow.com/questions/3480999/using-collate-in-android-sqlite-locales-is-ignored-in-like-statement – Mosbah Jan 25 '19 at 09:35
  • The contact are handles by android, there are not in my database (ccontentResolver). I want the search "jer" or "jér" to match contacts "Jérome" or "Jérome" – Nicolas Jan 25 '19 at 10:32

1 Answers1

1

Use the CONTENT_FILTER_URI API to quickly search contacts by name, this should handle accents automatically for you:

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();
marmor
  • 25,207
  • 10
  • 99
  • 145