alaa
alaa

Reputation: 43

How to get text from searchview?

How can I get the text from Searchview in this case this is Search method

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

and this is Searchview Listener, when I put searchview.query().toString in search method the application cracked ,How can I solve this problem ?

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;
        }
    });

Upvotes: 2

Views: 5682

Answers (4)

Gowtham Subramaniam
Gowtham Subramaniam

Reputation: 3478

Try this code:

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;
    }
});

Upvotes: 0

Farwa
Farwa

Reputation: 7024

If you want to take the text from the searchView it's basically the newText from the method onQueryTextChange.

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

Upvotes: 1

Imtiaz Dipto
Imtiaz Dipto

Reputation: 352

use this way to get string from 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;
        }
    });

Upvotes: 0

Ramesh sambu
Ramesh sambu

Reputation: 3539

Try this

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

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

Instead

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

Upvotes: 3

Related Questions