Create “history” to SearchView on ActionBar

匿名 (未验证) 提交于 2019-12-03 02:01:02

问题:

I want to have history on my SearchView, I've been googling around, and the only sensible(?) tutorial I found was this, but that's just for like Gingerbread, not API>14.

Then I found this code:

And that code only works half, since it doesn't show results from what you've already written, it shows all the items.

I just want it to look like this:

This is my current code for adding the SearchView:

res/menu/menu.xml:

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

Could someone please just give me something to start with, to be honest I have no idea where to start. So any help would be really appreciated.

回答1:

This page talks about how you can implement history for SearchView.

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

First, you have to create a content provider:

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

Then declare the content provider in your application manifest like this:

     ... 

Then add the content provider to your searchable configurations like this:

You can call saveRecentQuery() to save queries at any time. Here's how you can do it in the onCreate method for your activity:

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

To clear search history you simply need to call SearchRecentSuggestions's method clearHistory() like this:

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


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