Android - SimpleAdapter过滤查询,如何忽略重音?

发布于 2024-12-19 16:17:14 字数 1253 浏览 8 评论 0原文

我目前正在使用联系人的 ContentProvider 从设备中检索联系人,并允许用户通过在 EditText 中键入内容来过滤结果。

为此,我在 SimpleAdapter 上设置了一个过滤器查询,如下所示:

    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); 

这在大多数情况下都有效,但是当我的联系人带有口音时(示例:Tést Contact),那么我希望即使用户键入,该联系人也会显示测试接触,目前还没有。

此外,在这种情况下,大小写也不会被忽略,而对于标准字符来说,例如,如果我有一个名为 Omar 的联系人并搜索 omar,它会匹配,但如果我有一个名为 Ómar 的联系人并搜索 ómar,它会不匹配。

有谁知道我应该做什么来实现我想要实现的行为?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尴尬癌患者 2024-12-26 16:17:14

我会在这里看到 2 个选项:

  • 创建一个表,其中包含联系人姓名的无重音版本以及对实际联系人 ID 的引用
  • 将重音字符替换为 ?在您的搜索中(这可能会导致用户真正不期望的行为,但要简单得多)

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)
魔法唧唧 2024-12-26 16:17:14

请参阅我的问题 在 Android SQLite 中使用 COLLATE - 区域设置在 LIKE 语句中被忽略

简短回答 - 我认为不可能在 Android SQLite 中使用 LIKE 语句并忽略重音。我通过在数据库中创建一个新列来解决这个问题,在其中存储相同的名称,不带重音且采用小写字母。例如,第 1 列存储“Tést Contact”——用于显示,第 2 列存储“test contact”——用于使用 LIKE 语句。
Android 2.3 有一个 Normalizer 类,它将删除字符串中的所有重音符号。如果您支持较低的 Android API,那么您可能需要以某种方式编写自己的标准化器......

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...

海之角 2024-12-26 16:17:14

您可以使用替换功能来删除重音字符。看看这个简单的解决方案:

如何当列中有重音字符时,SQL 会比较列吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文