How to use searchable.xml

て烟熏妆下的殇ゞ 提交于 2021-01-29 04:04:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!