2013-06-21 69 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); 
    } 
} 
相關問題