Avoid leaving the gridview hidden when there are no results in the search

1

In my app, implement a SearchView. I managed to make it work perfectly but I had several doubts when I saw it work:

I need a mechanism for when there are no results I show a textview that there is not ... so I did the following

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { 

    @Override 
    public boolean onQueryTextSubmit(String query) { 
        return false; 
    }

    @Override 
    public boolean onQueryTextChange(String newText) { 

        serviceAdapter.getFilter().filter(newText); 

        if (serviceAdapter.getCount() <1) { 
            findViewById(R.id.tvNoResult).setVisibility(View.VISIBLE); 
            gridView.setVisibility(View.GONE); 
        }else{ 
            findViewById(R.id.tvNoResult).setVisibility(View.GONE); 
            gridView.setVisibility(View.VISIBLE); 
        } 

        return true; 
    } 
}); 

I added a hidden TextView in my xml and in the java I made a condition that said if the adapter was less than 1 it would hide the GridView and show the TextView, everything worked as expected but the problem is that when you press the back button or the one to close, the SearchView I am left the TextView visible and then it hinders me the operation:

This is how it looks like later:

My question is: is there a method to work with the back button and close the SearchView? What can I do to prevent this from happening?

    
asked by cjamdeveloper 25.01.2018 в 06:12
source

1 answer

1

What you have to do is to make visible the GridView when you press the back button, depending on which button presses the place where you should make visible the GridView will be different.

For example, if you press the back key of the device, you will have to overwrite the method onBackPressed() , which is the one executed when the key is pressed.

@Override
public void onBackPressed() {
    super.onBackPressed();
    findViewById(R.id.tvNoResult).setVisibility(View.GONE); 
    gridView.setVisibility(View.VISIBLE);
}

And if you are pressing the date to go back from the action bar of the application, you will have to overwrite the onOptionsItemSelected() method that is executed when you press one of the options in the action bar. The id of the option to go backwards ⬅ is android.R.id.home .

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (item.getItemId() == android.R.id.home) {
        findViewById(R.id.tvNoResult).setVisibility(View.GONE); 
        gridView.setVisibility(View.VISIBLE);
    }

    return super.onOptionsItemSelected(item);
}

Update

To make the gridView visible when you preface the X of the SeachView you must implement the interface OnCloseListener of this.

searchView.setOnCloseListener(new SearchView.OnCloseListener() {
    @Override
    public boolean onClose() {

        findViewById(R.id.tvNoResult).setVisibility(View.GONE); 
        gridView.setVisibility(View.VISIBLE); 
        return false;
    }
});

You can find a simple example of how to do it here .

    
answered by 25.01.2018 в 20:47