我试图让搜索结果显示用户开始输入时先前输入的搜索结果。
我已经按照http://developer.android.com/guide/topics/search/adding-recent-query-suggestions.html处的所有说明进行了检查,并环顾了其他各个地方,但我似乎无法让它正常工作。
我在我的应用程序中使用searchview查看片段,该片段允许用户查找特定停靠点的公交时刻表。代码如下。
transit.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_search"
android:title="Search"
android:icon="@android:drawable/ic_menu_search"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView"
android:searchSuggestAuthority="com.teamzeta.sfu.utilities.BusSuggestionProvider"
android:searchSuggestSelection=" ?"/>
</menu>
BusSuggestionProvider
package com.teamzeta.sfu.utilities;
import android.content.SearchRecentSuggestionsProvider;
public class BusSuggestionProvider extends SearchRecentSuggestionsProvider{
public final static String AUTHORITY = "com.teamzeta.sfu.utilities.BusSuggestionProvider";
public final static int MODE = DATABASE_MODE_QUERIES;
public BusSuggestionProvider() {
setupSuggestions(AUTHORITY, MODE);
}
}
searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/stop_number"
android:searchSuggestAuthority="com.teamzeta.sfu.utilities.BusSuggestionProvider"
android:searchSuggestSelection=" ?">
</searchable>
NextBusFragment onCreateOptionsMenu
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.transit, menu);
MenuItem searchViewItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchViewItem.getActionView();
searchView.setIconifiedByDefault(false);
searchView.setInputType(InputType.TYPE_CLASS_NUMBER);
searchView.setQueryHint(getResources().getString(R.string.stop_number));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
search(s);
SearchRecentSuggestions suggestions = new SearchRecentSuggestions(getActivity(), BusSuggestionProvider.AUTHORITY, BusSuggestionProvider.MODE);
suggestions.saveRecentQuery(s, null);
return false;
}
@Override
public boolean onQueryTextChange(String s) {
return false;
}
});
}
Manifest Provider
<provider android:name="com.teamzeta.sfu.utilities.BusSuggestionProvider"
android:authorities="com.teamzeta.sfu.utilities.BusSuggestionProvider" />
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
search()
方法关闭并从API检索总线列表,然后在列表视图中将其显示在屏幕上。
有谁可以弄清楚为什么这不显示历史?
非常感谢!
答案 0 :(得分:0)
您需要将您的活动设置为&#34;可搜索&#34;在你的清单中:
<activity>
...
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>