Android SearchRecentSuggestions - 当在SearchView中输入时,建议不会显示

21

我有一个工作中的搜索小部件,想要添加搜索历史建议。我遵循了Android教程(http://developer.android.com/guide/topics/search/adding-recent-query-suggestions.html),虽然搜索仍然可以正常工作,但没有显示任何建议。这是我的代码:

  1. 内容提供程序

package com.mypackage;

import android.content.SearchRecentSuggestionsProvider;

public class SearchHistoryProvider extends SearchRecentSuggestionsProvider {
    public final static String AUTHORITY = SearchHistoryProvider.class.getName();
    public final static int MODE = DATABASE_MODE_QUERIES;

    public SearchHistoryProvider() {
        setupSuggestions(AUTHORITY, MODE);
    }
}
  • 在 Manifest 中声明提供程序

  • <provider 
        android:name=".SearchHistoryProvider"
        android:authorities="com.mypackage.SearchHistoryProvider">
    </provider>
    
  • 可搜索的配置

    <?xml version="1.0" encoding="utf-8"?>
    <searchable xmlns:android="http://schemas.android.com/apk/res/android"
        android:label="@string/app_name"
        android:hint="@string/search_hint"
        android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
        android:searchSuggestAuthority="com.mypackage.SearchHistoryProvider"
        android:searchSuggestSelection=" ?">
    </searchable>
    
  • 将查询保存到内容提供者(在我的可搜索活动中)

    private void handleIntent(Intent intent) {
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
    
        String query = intent.getStringExtra(SearchManager.QUERY);
        SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
                            SearchHistoryProvider.AUTHORITY, SearchHistoryProvider.MODE);
        suggestions.saveRecentQuery(query, null);
    
        // Collapse the search view as a search is performed
        MenuItem searchItem = mMenu.findItem(R.id.search);
        SearchView searchView = (SearchView) mMenu.findItem(R.id.search).getActionView();
        searchItem.collapseActionView();
        searchView.setQuery("", false);
    
        // send the query to the global search activity for loading the data
        Intent globalSearchIntent = new Intent(this, GlobalSearchFragmentActivity.class);
        GroceryOTGUtils.copyIntentData(intent, globalSearchIntent);
        globalSearchIntent.putExtra(GlobalSearchFragmentActivity.GLOBAL_SEARCH, true);
        startActivity(globalSearchIntent);
    }
    }
    

    除了建议没有真正显示出来(搜索看起来与我添加它们之前一样),其他都正常工作。任何帮助将不胜感激!


  • 啊,我解决了这个问题。这是 Android 清单配置中的错误(提供程序名称)。我该如何取消悬赏? - Eng.Fouad
    @Eng.Fouad 如果您为其他问题设置了赏金,那么您就无法取消该赏金! - LOG_TAG
    @Eng.Fouad的管理员可以在极端情况下退还悬赏金,您可以尝试标记此帖子,选择“其他”并陈述您的情况。现在还不算太晚。 :) - Shadow The Spring Wizard
    1
    @Eng.Fouad 你能简要描述一下你的解决方案吗?这样对其他人会有帮助。 - Paul Lammertsma
    1个回答

    3
    我认为问题在于,在 Manifest.xml 中声明的 SearchHistoryProvider 是错误的。
    android:name=".SearchHistoryProvider"
    

    写 .$NAME 是 $DEFAULTPACKAGE.$NAME 的简写方式,因此如果您的 $DEFAULTPACKAGE 是 'com.myapp' 并且您将 Provider 的 android:name 写为 .SearchHistoryProvider,则 Android 将无法找到它,因为它位于 com.mypackage 中,如下面代码的第一行所示。

    package com.mypackage;
    
    import android.content.SearchRecentSuggestionsProvider;
    
    public class SearchHistoryProvider extends SearchRecentSuggestionsProvider {
        public final static String AUTHORITY = SearchHistoryProvider.class.getName();
        public final static int MODE = DATABASE_MODE_QUERIES;
    
        public SearchHistoryProvider() {
            setupSuggestions(AUTHORITY, MODE);
        }
    }
    

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接