Search code examples
javaandroidsortingandroid-recyclerviewsearchview

RecyclerView cannot be sorted after using SearchView filter


I'm trying to implement a simple searching button using a SearchView to find items in a RecyclerView. However, this searching button breaks my sorting functions as the RecyclerView can no longer be sorted after typing in the SearchView.

Here is my Activity

    private void sortAscending() {
        Collections.sort(stationList, (station1, station2) -> {
            if (station1.getData().getCurrent().getPollution().getAqius() > station2.getData().getCurrent().getPollution().getAqius()) {
                return 1;
            } else if (station1.getData().getCurrent().getPollution().getAqius() < station2.getData().getCurrent().getPollution().getAqius()) {
                return -1;
            } else {
                return 0;
            }
        });
    }

    private void sortDescending() {
        Collections.sort(stationList, (station1, station2) -> {
            if (station1.getData().getCurrent().getPollution().getAqius() > station2.getData().getCurrent().getPollution().getAqius()) {
                return -1;
            } else if (station1.getData().getCurrent().getPollution().getAqius() < station2.getData().getCurrent().getPollution().getAqius()) {
                return 1;
            } else {
                return 0;
            }
        });
    } 


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //Inflate the menu here
        getMenuInflater().inflate(R.menu.list_menu, menu);
        MenuItem searchItem = menu.findItem(R.id.item_search);
        SearchView searchView = (SearchView) searchItem.getActionView();
        searchView.setOnQueryTextListener(this);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //A String for the message to be displayed in a Toast
        String msg = "";
        //Switch and case on the MenuItem object's id
        switch (item.getItemId()) {
            case R.id.sort_ascending:
                msg = "sorting by ascending.";
                sortAscending();
                Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
                break;
            case R.id.sort_descending:
                msg = "sorting by descending.";
                sortDescending();
                Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
                break;
        }
        adapter.notifyDataSetChanged();
        return super.onOptionsItemSelected(item);
    }
    @Override
    public boolean onQueryTextChange(String newText) {
        filter(newText.toLowerCase());
        return true;
    }

    private void filter(String text) {
        ArrayList<Station> filteredList = new ArrayList<>();

        for (Station item : stationList) {
            if (item.getData().getCity().toLowerCase().contains(text.toLowerCase())) {
                filteredList.add(item);
            }
        }

        adapter.filterList(filteredList);
        adapter.notifyDataSetChanged();
    }
    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    } 

Now I suspect it has something to do with the onOptionsItemSelected method because I don't have anything in there that does something when the user clicks on the search button on the menu.

Here's the Adapter class for the RecyclerView, removed other functions for simplicity.

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        //...set text and information from ArrayList
        //Launch an activity and user clicks on an item in RecyclerView
        holder.itemView.setOnClickListener(view -> {
            Intent intent = new Intent(context, CityActivity.class);
            context.startActivity(intent);
        });
    }
//Function to set the ArrayList to the filtered list
//filteredList ArrayList is passed in from filter() in Activity
    public void filterList(ArrayList<Station> filteredList) {
        stationList = filteredList;
    } 

And here is the XML for the menu:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/item_sort"
        android:icon="@drawable/ic_sort_black_24dp"
        android:title="Sort"
        app:showAsAction="ifRoom">

        <menu>
            <item android:id="@+id/sort_ascending"
                android:title="Sort by ascending"/>
            <item android:id="@+id/sort_descending"
                android:title="Sort by descending"/>
        </menu>
</item>

    <item android:id="@+id/item_search"
        android:icon="@drawable/ic_search_black_24dp"
        android:title="Search"
        app:showAsAction="always"
        app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu> 

I'm having trouble figuring out why after entering information into the SearchView and removing it so the RecyclerView returns to its normal state, I cannot use the sort functions (ascending and descending) to sort the RecyclerView.


Solution

  • I have fixed my problem by checking if the text length is zero, which means no text is entered and user is not using the search. Then I just call the filterList function to set the current stationList as the one to display.

        private void filter(String text) {
            if (text.length() == 0) {
                adapter.filterList(stationList);
            } else {
                ArrayList<Station> filteredList = new ArrayList<>();
    
                for (Station item : stationList) {
                    if (item.getData().getCity().toLowerCase().contains(text.toLowerCase())) {
                        filteredList.add(item);
                    }
                }
    
                adapter.filterList(filteredList);
            }
        } 
    

    I also moved the adapter.notifyDataSetChanged(); into the filterList function for clarity.