2

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;
        }
    });
Prags
  • 2,457
  • 2
  • 21
  • 38
alaa
  • 43
  • 1
  • 9

4 Answers4

3

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());
Ramesh sambu
  • 3,577
  • 2
  • 24
  • 39
1

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;
    }
Farwa
  • 6,156
  • 8
  • 31
  • 46
0

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;
        }
    });
Imtiaz Dipto
  • 342
  • 1
  • 3
  • 9
  • the application stopped and this is the error in logcat Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.SearchView.setOnQueryTextListener(android.support.v7.widget.SearchView$OnQueryTextListener)' on a null object reference – alaa Jan 18 '18 at 05:29
0

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;
    }
});
Gowtham Subramaniam
  • 3,358
  • 2
  • 19
  • 31