How to show search view recent search history

﹥>﹥吖頭↗ 提交于 2019-12-12 17:16:26

问题


I tried to using following ways but unable to show recent search history

Create "history" to SearchView on ActionBar

Implementing SearchView in action bar

kindly help how to create and show recent search in serachview searchable xml


回答1:


These are step i am using

 Searchable xml file 

  <?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="example.com.cle.MySuggestionProvider"
    android:searchSuggestSelection=" ?"
    >
</searchable>




Provider 


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


Activity in manifiest

 <activity
            android:name=".BaseActivity"
            android:configChanges="orientation|screenSize|keyboardHidden"
            android:theme="@style/AppTheme.ActionBar">


            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>


            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />


Activity class

  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


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


        }
    }


Content provider class 

public final static String AUTHORITY ="example.com.cle.MySuggestionProvider";
    public final static int MODE = DATABASE_MODE_QUERIES;

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



回答2:


I got the solution

1.create xml folder searchable

    <?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="your packagename.MySuggestionProvider"
    android:searchSuggestSelection=" ?" >
</searchable>

2.create content provider class extends SearchRecentSuggestionsProvider

 public class MySuggestionProvider extends SearchRecentSuggestionsProvider {

    public final static String AUTHORITY = "your package name.MySuggestionProvider";
    public final static int MODE = DATABASE_MODE_QUERIES;

    public MySuggestionProvider() {
        setupSuggestions(AUTHORITY, MODE);
    }
}
  1. in your activity class

oncreate method

 handledearch(getIntent());

4. over ride method


    @Override
    protected void onNewIntent(Intent intent) {
       setIntent(intent);

      handledearch(intent);
    }

    public void handledearch( Intent intent)
    {
        if (Intent.ACTION_SEARCH.equalsIgnoreCase(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);

        mtToolbar.setTitle(query);

            SearchRecentSuggestions searchRecentSuggestions=new SearchRecentSuggestions(this,
                    MySuggestionProvider.AUTHORITY,MySuggestionProvider.MODE);

            searchRecentSuggestions.saveRecentQuery(query,null);
        }
    }
  1. in the menu

    @Override public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.menu,menu);
    
        SearchManager searchManager=(SearchManager)getSystemService(Context.SEARCH_SERVICE);
    
        SearchView searchView;
    
        MenuItem menuItem=menu.findItem(R.id.action_search);
    
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
        {
            searchView=(SearchView)menuItem.getActionView();
    
        }
        else
        {
            searchView=(SearchView) MenuItemCompat.getActionView(menuItem);
    
        }
    
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setQueryHint(getResources().getString(R.string.search_hint));
    
        return super.onCreateOptionsMenu(menu);
    
    }
    


来源:https://stackoverflow.com/questions/38368873/how-to-show-search-view-recent-search-history

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