【问题标题】:recent searches are not displayed, custom suggestions are不显示最近的搜索,自定义建议是
【发布时间】:2012-07-04 23:37:16
【问题描述】:

我有一个搜索小部件 (SearchView),在键入时显示自定义建议。我已关注 official guide 将最近的查询添加到建议中,但我仍然只有自定义建议。
对于每次搜索,我的片段都会调用此函数(已验证):

private void addQueryToRecent(String query) {
    SearchRecentSuggestions suggestions = new SearchRecentSuggestions(getActivity(),
            MyCustomSuggestionProvider.AUTHORITY,
            MyCustomSuggestionProvider.MODE);
    suggestions.saveRecentQuery(query, "recent");
}

我的建议提供者似乎还可以:

public class MyCustomSuggestionProvider extends SearchRecentSuggestionsProvider {

public final static String AUTHORITY = "com.zgui.musicshaker.provider.MyCustomSuggestionProvider";
public final static int MODE = DATABASE_MODE_QUERIES | DATABASE_MODE_2LINES;

public MyCustomSuggestionProvider() {
    setupSuggestions(AUTHORITY, MODE);
}

@Override
public Cursor query(Uri uri, String[] projection, String sel,
        String[] selArgs, String sortOrder) {
    //retrieves a custom suggestion cursor and returns it
}
}

可搜索的.xml:

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="artist, track informations..."
android:label="@string/app_name"
android:queryAfterZeroResults="true"
android:searchSuggestAuthority="com.zgui.musicshaker.provider.MyCustomSuggestionProvider"
android:searchSuggestSelection=" ?" />

清单:

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>

        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
    </activity>
<provider
        android:name=".provider.MyCustomSuggestionProvider"
        android:authorities="com.zgui.musicshaker.provider.MyCustomSuggestionProvider"
        android:enabled="true" >
    </provider>

根据that post,我应该在 data/data/app.package.name/databases/databasename.db 有一个最近的搜索数据库,但我似乎没有...
或者,也许我应该自己在 MyCustomSuggestionProvider.query() 返回的光标中添加“最近的搜索建议”?欢迎任何想法......

【问题讨论】:

  • 你没有使用 SearchSuggestionProvider,怎么会?

标签: android search-suggestion


【解决方案1】:

找到了:Android 文档上根本不清楚,但我需要自己提出请求并在 MyCustomSuggestionProvider.query() 中合并游标:

Cursor recentCursor = super.query(uri, projection, sel, selArgs,
            sortOrder);
Cursor[] cursors = new Cursor[] { recentCursor, customCursor};
return new MergeCursor(cursors);

请确保您在两者中都有相同的列...

【讨论】:

  • 您是如何确定如何以相同方式匹配列的?
  • 你的 customCursor 是什么样的?
  • “确保两列中的列相同”是什么意思?
【解决方案2】:
private static final String[] SEARCH_SUGGEST_COLUMNS = {
        SearchManager.SUGGEST_COLUMN_FORMAT,
        SearchManager.SUGGEST_COLUMN_ICON_1,
        SearchManager.SUGGEST_COLUMN_TEXT_1,
        SearchManager.SUGGEST_COLUMN_INTENT_DATA,
        BaseColumns._ID };

以上是最近搜索建议数据库中使用的列列表,为您的自定义搜索建议使用相同的列表以避免冲突

【讨论】:

  • 我不认为所有这些参数都是必需的。
【解决方案3】:

为了扩展 elgui 的答案,下面是我如何匹配列的示例:

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        final Cursor recentsCursor = super.query(uri, projection, selection, selectionArgs, sortOrder);
        final Cursor customResultsCursor = queryCache(recentsCursor, selectionArgs[0]);
        return new MergeCursor(new Cursor[]{recentsCursor, customResultsCursor});
    }

    private Cursor queryCache(Cursor recentsCursor, String userQuery) { 
        final MatrixCursor arrayCursor = new MatrixCursor(recentsCursor.getColumnNames());

        final int formatColumnIndex = recentsCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_FORMAT);
        final int iconColumnIndex = recentsCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_ICON_1);
        final int displayColumnIndex = recentsCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1);
        final int queryColumnIndex = recentsCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_QUERY);
        final int idIndex = recentsCursor.getColumnIndex("_id");

        final int columnCount = recentsCursor.getColumnCount();

        // Populate data here
        int startId = Integer.MAX_VALUE;

        for (String customSearchResult : customSearchResults) {
            final Object[] newRow = new Object[columnCount];
            if (formatColumnIndex >= 0) newRow[formatColumnIndex] = 0;
            if (iconColumnIndex >= 0) newRow[iconColumnIndex] = R.drawable.invisible;
            if (displayColumnIndex >= 0) newRow[displayColumnIndex] = customSearchResult;
            if (queryColumnIndex >= 0) newRow[queryColumnIndex] = customSearchResult;
            newRow[idIndex] = startId--;
            arrayCursor.addRow(newRow);
        }       

        return arrayCursor;
    }

由于这是基于 SearchRecentSuggestionsProvider 的实现细节,它在其他情况下仍然可能失败,编写自己的最近历史记录并一起做可能更值得。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-20
    • 1970-01-01
    相关资源
    最近更新 更多