Android - SimpleAdapter filter query, how to ignore accents?

ε祈祈猫儿з 提交于 2019-12-10 10:55:44

问题


I am currently using the ContentProvider for contacts to retrieve the contacts from the device and allowing the user to filter the results by typing into an EditText.

To do so I have set up a filter query on a SimpleAdapter as follows:

    contactsAdapter.setFilterQueryProvider(new FilterQueryProvider() {
        String[] PROJECTION = new String[] {
                ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.Contacts.HAS_PHONE_NUMBER,
        };
        String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED COLLATE NOCASE ASC";

          public Cursor runQuery(CharSequence constraint) {
            String SELECTION = "LOWER(" + ContactsContract.Contacts.DISPLAY_NAME + ")" 
            + " LIKE '" + constraint + "%' " + "and " + ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'";
            Cursor cur = managedQuery(ContactsContract.Contacts.CONTENT_URI, 
                    PROJECTION, SELECTION, null, sortOrder);
            return cur; 
          }
        });
    setListAdapter(contactsAdapter); 

This works in most cases however when I have a contact with an accent (Example: Tést Cóntact) then I want the contact to show up even if the user types in Test Contact, currently it doesn't.

Also the case is not ignored in this case either whereas for standard characters it is, for example if I have a contact called Omar and search for omar it matches but if I have a contact called Ómar and search for ómar it doesn't match.

Does anyone know what I should be doing to implement the behavior I want to achieve?


回答1:


I would see 2 options here :

  • Create a table that contains accent-less version of the contacts names and a reference to the actual contact Id
  • Replace accented caracters by ? in your search (which may result in not really user expected behaviour, but is so much simpler)



回答2:


See my question Using COLLATE in Android SQLite - Locales is ignored in LIKE statement

Short answer - I think it's impossible to use the LIKE statement in Android SQLite and ignore accents. I solved it by making a new column in the database, where you store the same name without accents and in lower case. For example Column 1 stores "Tést Cóntact" - which is used for display and Column 2 stores "test contact" - which is used for using the LIKE statement. Android 2.3 has a Normalizer class which will remove all accents from a string. If you are supporting lower Android API, then you may need to write your own normalizer somehow...




回答3:


You can use the replace function to remove the accented characters. Look at this simple solution:

How to SQL compare columns when one has accented chars?



来源:https://stackoverflow.com/questions/8400227/android-simpleadapter-filter-query-how-to-ignore-accents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!