9

入力中にカスタム提案を表示する検索ウィジェット (SearchView) があります。公式ガイドに従って最近のクエリを提案に追加しましたが、まだカスタム提案しかありません。
検索ごとに、私のフラグメントはこの関数を呼び出します (検証済み):

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

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

その投稿によると、data/data/app.package.name/databases/databasename.db に最近の検索データベースがあるはずですが、ないようです...
または、追加することになっているのかもしれませんMyCustomSuggestionProvider.query() が返すカーソル内の「最近の検索候補」どんなアイデアでも大歓迎です...

4

3 に答える 3

8

見つかりました: Android ドキュメントではまったく明確ではありませんでしたが、自分でリクエストを作成し、MyCustomSuggestionProvider.query() でカーソルをマージする必要があります。

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

ただし、両方に同じ列があることを確認してください...

于 2012-07-04T15:29:01.540 に答える
2

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 の実装の詳細に基づいているため、他の状況ではまだ失敗する可能性があり、独自の最近の履歴を作成して一緒に実行する方が価値がある場合があります。

于 2014-10-17T01:01:54.843 に答える