Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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!

like image 715
abeliangrape Avatar asked Jun 21 '13 16:06

abeliangrape


1 Answers

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);
    }
}
like image 80
fabian Avatar answered Dec 06 '22 15:12

fabian