最近的搜索未显示,自定义建议

elg*_*gui 8 android search-suggestion

我有一个搜索小部件(SearchView),在输入时显示自定义建议.我已经按照官方指南将最近的查询添加到建议中,但我仍然只有自定义建议.
对于每次搜索,我的片段调用此函数(已验证):

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

我的建议提供者似乎没问题:

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
}
}
Run Code Online (Sandbox Code Playgroud)

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=" ?" />
Run Code Online (Sandbox Code Playgroud)

清单:

            <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>
Run Code Online (Sandbox Code Playgroud)

根据那篇文章,我应该在data/data/app.package.name/databases/databasename.db上有一个最近的搜索数据库,但我似乎没有...
或者我可能应该添加"最近的搜索建议"我自己在MyCustomSuggestionProvider.query()返回的游标中?欢迎任何想法......

elg*_*gui 6

发现它:在Android文档中根本不清楚,但我需要自己发出请求并在MyCustomSuggestionProvider.query()中合并游标:

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

确保两者都有相同的列...

  • 你是怎么弄清楚如何匹配列相同的? (3认同)