How to get a text from SearchView?

后端 未结 2 545
星月不相逢
星月不相逢 2020-12-31 01:16

I need to get a text from SearchView and compare it to strings in my activity\'s ListView and show a Toast if the word in a SearchView is in my ListView. How do I do that? H

相关标签:
2条回答
  • 2020-12-31 01:42

    To get text of SearchView, use searchView.getQuery().

    0 讨论(0)
  • 2020-12-31 01:46

    try this code :

        MenuItem ourSearchItem = menu.findItem(R.id.menu_item_search);
    
        SearchView sv = (SearchView) ourSearchItem.getActionView();
    
        sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                Toast.makeText(getContext(), query, Toast.LENGTH_SHORT).show();
                return true;
            }
    
            @Override
            public boolean onQueryTextChange(String newText) {
    
                adapter.getFilter().filter(newText);
                }
                return false;
            }
        });
    
    0 讨论(0)
提交回复
热议问题