2013-10-03 37 views
3

我希望在我的SearchView上有歷史記錄,我一直在使用Google搜索,而我發現的唯一合理的(?)教程是this,但這僅僅適用於類似薑餅, 14。在ActionBar上創建「歷史記錄」到SearchView

然後我發現這個代碼:

String[] columnNames = {"_id","text"}; 
     MatrixCursor cursor = new MatrixCursor(columnNames); 
     String[] array = {"Snääälla", "bla bla bla", "Jävla piss"}; //if strings are in resources 
     String[] temp = new String[2]; 
     int id = 0; 
     for(String item : array){ 
      temp[0] = Integer.toString(id++); 
      temp[1] = item; 
      cursor.addRow(temp); 
     } 
     String[] from = {"text"}; 
     int[] to = {android.R.id.text1}; 
     CursorAdapter ad = new SimpleCursorAdapter(this.getActivity(), android.R.layout.simple_list_item_1, cursor, from, to); 
     mSearchView.setSuggestionsAdapter(ad); 

這代碼只能一半,因爲它並不顯示從你所編寫的程序的結果,它顯示了所有項目。

我只是希望它看起來是這樣的:

Google Play Store screnshot

這是我目前對加入搜索查看代碼:

RES /菜單/ menu.xml文件:

<item android:id="@+id/fragment_searchmenuitem" 
     android:icon="@drawable/ic_search_white" 
     android:title="@string/menu_search" 
     android:showAsAction="collapseActionView|ifRoom" 
     android:actionViewClass="android.widget.SearchView" /> 

MainActivity.java:

@Override 
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
    if(!mDrawerLayout.isDrawerOpen(mDrawerList)) { 
     inflater.inflate(R.menu.fragment_search, menu); 

     mMenuItem = menu.findItem(R.id.fragment_searchmenuitem); 
     mSearchView = (SearchView) mMenuItem.getActionView(); 
     mMenuItem.expandActionView(); 
     mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { 
      @Override 
      public boolean onQueryTextSubmit(String s) { 
       mMenuItem.collapseActionView(); 
       searchSupport.SearchForLyrics(s); 
       actionBar.setSubtitle("Searcing for: " + s); 
       return true; 
      } 

      @Override 
      public boolean onQueryTextChange(String s) { 
       return false; 
      } 
     }); 
    } 

    super.onCreateOptionsMenu(menu, inflater); 
} 

有人可以請給我一些開始,說實話,我不知道從哪裏開始。所以任何幫助將非常感激。

+0

你有沒有想過把它們保存在你的數據庫中,然後使用自動完成的textview使用光標適配器從數據庫中檢索它 –

+0

@TerrilThomas是的,是的,我有。但我希望谷歌會有一些官方的API或者其他的東西來做到這一點。 – GuiceU

回答

6

本頁面討論如何爲SearchView實現歷史記錄。

http://developer.android.com/guide/topics/search/adding-recent-query-suggestions.html

首先,你必須創建一個內容提供商:

public class MySuggestionProvider extends SearchRecentSuggestionsProvider { 
    public final static String AUTHORITY = "com.example.MySuggestionProvider"; 
    public final static int MODE = DATABASE_MODE_QUERIES; 

    public MySuggestionProvider() { 
     setupSuggestions(AUTHORITY, MODE); 
    } 
} 

然後聲明內容供應商在你的應用程序清單如下:

<application> 
    <provider android:name=".MySuggestionProvider" 
       android:authorities="com.example.MySuggestionProvider" /> 
    ... 
</application> 

然後添加內容提供商到像這樣的可搜索配置:

<?xml version="1.0" encoding="utf-8"?> 
<searchable xmlns:android="http://schemas.android.com/apk/res/android" 
    android:label="@string/app_label" 
    android:hint="@string/search_hint" 
    android:searchSuggestAuthority="com.example.MySuggestionProvider" 
    android:searchSuggestSelection=" ?" > 
</searchable> 

您可以隨時調用saveRecentQuery()來保存查詢。這裏是你如何能做到這在onCreate方法爲您的活動:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Intent intent = getIntent(); 

    if (Intent.ACTION_SEARCH.equals(intent.getAction())) { 
     String query = intent.getStringExtra(SearchManager.QUERY); 
     SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this, 
       MySuggestionProvider.AUTHORITY, MySuggestionProvider.MODE); 
     suggestions.saveRecentQuery(query, null); 
    } 
} 

要清除搜索歷史記錄,你只需要調用SearchRecentSuggestions的方法clearHistory()這樣的:

SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this, 
     HelloSuggestionProvider.AUTHORITY, HelloSuggestionProvider.MODE); 
suggestions.clearHistory(); 
+1

此解決方案不完整,需要與此混合使用:http://stackoverflow.com/questions/21585326/implementing-searchview-in-action-bar。如果發現最好實施一個自定義建議適配器,如果該應用程序以任何方式爲主題。 – 3c71