android - 如何显示搜索 View 最近的搜索历史

标签 android android-sqlite searchview android-search android-sharedpreferences

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

Create "history" to SearchView on ActionBar

Implementing SearchView in action bar

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

最佳答案

这些是我正在使用的步骤

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

关于android - 如何显示搜索 View 最近的搜索历史,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38368873/

相关文章:

android - onSuggestionClick 重新创建我的 Activity

Android:CollapsingToolbarLayout 和 SearchView,文本重叠

android - 为什么我的自定义 Actionbar 上的图像有不需要的左填充

java - 你如何在Android中获得手机的MCC和MNC?

java - 将 BLOB 类型检索为 Byte

java - 尝试从 SQLite 数据库获取数据时出现 NullPointerException/CursorOutOfBoundsException

android - SearchView 搜索项目时无法保留选中的复选框

无法直接访问 UI 的 Android AsyncTask

android - 更改粗体 html 文本的文本颜色

android - 无法将数据插入到 Android 中的 SQLite 数据库中