如何显示搜索视图最近的搜索历史

问题描述 投票:0回答:2

我尝试使用以下方式但无法显示最近的搜索历史

为 ActionBar 上的 SearchView 创建“历史记录”

在操作栏中实现 SearchView

请帮助如何在 serachview 可搜索 xml 中创建和显示最近的搜索

android android-sqlite searchview android-search android-sharedpreferences
2个回答
1
投票

我得到了解决方案

  1. create an xml file called

    searchable.xml
    (in res/xml)

     <?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. 创建一个扩展 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);
     }
    

    }

  3. 在你的活动课程中

    onCreate
    方法

     handledearch(getIntent());
    
  4. 重写这个方法

     @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);
         }
     }
    
  5. 在菜单中

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

0
投票

这些是我正在使用的步骤

 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);
    }
© www.soinside.com 2019 - 2024. All rights reserved.