Android SearchRecentSuggestions - suggestions do not get displayed when typing in SearchView

时光毁灭记忆、已成空白 提交于 2019-12-05 09:47:20

问题


I have a working search widget and want to add search history suggestions. I followed the Android tutorial (http://developer.android.com/guide/topics/search/adding-recent-query-suggestions.html), and while the search still works, no suggestions are being displayed. Here is my code:

  1. Content provider

    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. Declaring provider in Manifest

    <provider 
        android:name=".SearchHistoryProvider"
        android:authorities="com.mypackage.SearchHistoryProvider">
    </provider>
    
  3. Searchable configuration

    <?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. Saving the queries to the content provider (in my searchable Activity)

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

Everything works fine, except the suggestions do not actually show up (the search looks the same as before I added them). Any help would be greatly appreciated!


回答1:


I think the problem was, that the declaration of the SearchHistoryProvider in the Manifest.xml was wrong.

android:name=".SearchHistoryProvider"

Writing .$NAME is a shorthand for $DEFAULTPACKAGE.$NAME, so if your $DEFAULTPACKAGE is 'com.myapp' und you write .SearchHistoryProvider as the android:name of your Provider, Android will simply not find it, as it is located in com.mypackage, as the first line of the following code indicates.

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


来源:https://stackoverflow.com/questions/17240057/android-searchrecentsuggestions-suggestions-do-not-get-displayed-when-typing-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!