问题
I read lots of posts on this but I actually don't understand how it works: I created a searchable.xml in the xml folder:
Then I referenced it to my Manifest like this:
I can't figure out how can I use it in the search code, I tried with the SearchManager but without success:
SearchView searchView;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate( R.menu.main, menu);
MenuItem myActionMenuItem = menu.findItem( R.id.action_search);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) myActionMenuItem.getActionView();
ComponentName componentName = new ComponentName(this, MainActivity.class);
searchView.setSearchableInfo(searchManager.getSearchableInfo(componentName));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
EventBus.getDefault().post(new SearchQueryEvent(s));
return false;
}
});
return true;
}
来源:https://stackoverflow.com/questions/41512459/how-to-use-searchable-xml