如何从searchview获取文本?

时间:2018-01-18 05:13:27

标签: android text searchview

在这种情况下,如何从Searchview获取文本 这是搜索方法

public void search(String query){
    adapter = new CustomAdapter(getBaseContext(),contactOps.searchCursor(query));
    lv.setAdapter(adapter);
}

这是Searchview Listener,当我在搜索方法中放入searchview.query()。toString时,应用程序破解了,我该如何解决这个问题?

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            search(searchView.getQuery().toString());
            return false;
        }
    });

4 个答案:

答案 0 :(得分:3)

试试这个

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        search(newText);
        return false;
    }
});

相反

search(searchView.getQuery().toString());

答案 1 :(得分:1)

如果你想从searchView中获取文本,它基本上是方法 onQueryTextChange 中的 newText

 @Override
    public boolean onQueryTextChange(String newText) {
        //newText is the query you are searching 
        return false;
    }

答案 2 :(得分:0)

使用这种方式从searchview获取字符串:

searchview.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            if(newText.length() >= 1){
                search(newText);
            }
            return false;
        }
    });

答案 3 :(得分:0)

试用此代码:

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    @Override
    public boolean onQueryTextSubmit(String query) {
       //Here u can get the value "query" which is entered in the search box.
         return true;
        }

    @Override
    public boolean onQueryTextChange(String newText) {
        //This is your adapter that will be filtered
        return false;
    }
});