2

我正在使用 Android SearchRecentSuggestionsProviderSearchable Configuration在应用搜索中显示以前的内容,并希望将建议的响应限制为仅返回以 ? (用户输入的内容)

目前“p”返回电话、ipod、照片、快乐,而我所追求的只是电话和照片

像 % 这样的与 SQL 相当的东西,但我似乎无法让它工作。

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:hint="@string/product_search_hint"
    android:imeOptions="actionSearch"
    android:label="@string/app_label"
    android:searchSuggestAuthority="com.myapp.ProductSearchSuggestionProvider"
    android:searchSuggestSelection="word MATCH ?" >
</searchable>

试过:

android:searchSuggestSelection="WORD LIKE '%'?"
4

2 回答 2

1

If you were implementing a fully custom suggestion provider, applying customizations at this level would work as expected. However, under the covers, SearchRecentSuggestionsProvider queries its local table with the following hard-coded arguments (have a look):

  • selection = "display1 LIKE ?"
  • selectionArgs = ["%{query}%"]

where {query} is the search query (it is sandwiched between two percent signs so that any character match anywhere in the string counts). It ignores the value passed in for the selection string (which is what you are setting with the searchSuggestSelection parameter) and the args array (beyond checking if it is empty), so there really isn't much you can do from XML.

Because the implementation ignores incoming parameters, there is no easy fix or override. To save yourself duplicate coding, I would:

  1. Copy the entire implementation of SearchRecentSuggestionsProvider from the link above. Nothing the class does should be framework private, so a direct copy should compile.
  2. Modify the construction statement from the args array, inside the query() method, to be String like = selectionArgs[0] + "%" instead of String like = "%" + selectionArgs[0] + "%" (i.e. remove the first percent)
  3. Subclass ProductSearchSuggestionProvider from your new implementation instead.
于 2012-08-13T17:59:19.310 回答
0

您是否尝试过字符串连接?

word LIKE ? || '%'

||是 SQLite 中的字符串连接。

于 2012-08-13T17:02:59.427 回答