我尝试使用以下方式但无法显示最近的搜索记录
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/