【问题标题】:Android SearchRecentSuggestions - suggestions do not get displayed when typing in SearchViewAndroid SearchRecentSuggestions - 在 SearchView 中输入时不显示建议
【发布时间】:2013-06-18 20:51:39
【问题描述】:

我有一个可用的搜索小部件,并想添加搜索历史建议。我遵循了 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);
        }
    }
    
  2. 在 Manifest 中声明提供者

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

    <?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>
    
  4. 将查询保存到内容提供者(在我的可搜索活动中)

    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 版主可以在极端情况下退还赏金,您可以尝试标记此帖子,选择“其他”并承诺您的情况。还不算晚。 :)
  • @Eng.Fouad 您能否在答案中简要描述您的解决方案,以便对其他人有所帮助?

标签: java android search search-suggestion


【解决方案1】:

我认为问题在于,SearchHistoryProvider 的声明 Manifest.xml 中的错误。

android:name=".SearchHistoryProvider"

.$NAME 是 $DEFAULTPACKAGE.$NAME,所以如果您的 $DEFAULTPACKAGE 是“com.myapp” 并且您将 .SearchHistoryProvider 写为您的 android:name Provider,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);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    • 1970-01-01
    • 2023-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多