2013-06-21 73 views
19

我有一个可用的搜索小部件,并希望添加搜索历史记录建议。我遵循Android教程(http://developer.android.com/guide/topics/search/adding-recent-query-suggestions.html),虽然搜索仍然有效,但没有任何建议正在显示。这里是我的代码:Android SearchRecentSuggestions - 在输入时不会显示建议SearchView

  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. 在清单

    <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. 声明提供商保存查询到内容提供商(在我搜索的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); 
    } 
    } 
    

一切工作正常,但建议不实际显示(搜索看上去和以前一样我加入他们)。任何帮助将不胜感激!

+0

嗯,我解决了这个问题。这是Android清单配置(提供者的名称)中的一个错误。我如何删除赏金? –

+0

@ Eng.Fouad如果你为他人提供赏金,你不能删除赏金! –

+0

@ Eng.Fouad版主可以在极端情况下退还赏金,您可以尝试标记此帖子,选择“其他”并保证您的情况。还不算太晚。 :) –

回答

2

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

android:name=".SearchHistoryProvider" 

写作$ NAME是 $ DEFAULTPACKAGE速记$ NAME,所以如果你的$ DEFAULTPACKAGE是 'com.myapp' UND你写.SearchHistoryProvider由于Android:。你 提供商的名称,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); 
    } 
} 
相关问题