我正在使用Wiki API在recyclerView中显示搜索。我有一个搜索栏,我想在用户键入任何查询时显示最近的搜索。我已经创建了提供程序类,还实现了handleIntent()并在main方法中对其进行了调用。
MainActivity:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_text_item,menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
final MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
//final SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false); // Do not iconify the widget;
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
fetchPages(query);
// Reset SearchView
searchView.clearFocus();
searchView.setQuery("", false);
searchView.setIconified(true);
searchItem.collapseActionView();
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
return true;
}
});
return true;
}
@Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
}
private void handleIntent(Intent intent) {
//store the search and display it
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
//get search query
searchQuery = intent.getStringExtra(SearchManager.QUERY);
//save search
SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
MySuggestionProvider.AUTHORITY, MySuggestionProvider.MODE);
suggestions.saveRecentQuery(searchQuery, null);
//set search to the textview
//mTextView.setText(searchQuery);
} else if (Intent.ACTION_VIEW.equals(intent.getAction())) {
// Handle a suggestions click (because the suggestions all use ACTION_VIEW)
searchQuery = intent.getStringExtra(SearchManager.QUERY);
//set search term to the textview
// mTextView.setText(searchQuery);
}
}
提供者:
public class MySuggestionProvider extends SearchRecentSuggestionsProvider {
public final static String AUTHORITY = "wiki.com.wikisearch.contentProvider.MySuggestionProvider";
public final static int MODE = DATABASE_MODE_QUERIES| DATABASE_MODE_2LINES;
public MySuggestionProvider() {
setupSuggestions(AUTHORITY, MODE);
}
}
清单:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
<activity android:name=".WbViewActivity"/>
<provider
android:authorities="wiki.com.wikisearch.contentProvider.MySuggestionProvider"
android:name=".contentProvider.MySuggestionProvider"/>
我不明白为什么在搜索栏中键入内容时没有得到任何建议,我错过了什么吗?