java - 使用SearchView过滤器后RecyclerView无法排序

标签 java android sorting android-recyclerview searchview

我正在尝试使用 SearchView 实现一个简单的搜索按钮来查找 RecyclerView 中的项目。但是,这个搜索按钮破坏了我的排序功能,因为在 SearchView 中输入内容后 RecyclerView 无法再排序。

这是我的 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;
    } 

现在我怀疑它与 onOptionsItemSelected 方法有关,因为当用户单击菜单上的搜索按钮时,我没有任何东西可以执行某些操作。

这是 RecyclerView 的 Adapter 类,为了简单起见,删除了其他函数。

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

这是菜单的 XML:

<?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> 

我无法弄清楚为什么在将信息输入 SearchView 并将其删除以使 RecyclerView 返回正常状态后,我无法使用排序函数(升序和降序)对 RecyclerView 进行排序。

最佳答案

我通过检查文本长度是否为零解决了我的问题,这意味着没有输入文本并且用户没有使用搜索。然后我只需调用 filterList 函数将当前 stationList 设置为要显示的站列表。

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

为了清楚起见,我还将 adapter.notifyDataSetChanged(); 移至 filterList 函数中。

关于java - 使用SearchView过滤器后RecyclerView无法排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53135873/

相关文章:

java - 如何从 xml 获取字符串数组到 fragment 的 java 文件?

java - 我必须打印数组,但每个循环都不起作用

Java : Protected access restriction for subclass on superclass object

android - 在 Android 中加载 fragment 需要更多时间

javascript - 在 JavaScript 中按定义值数组列表对对象数组进行排序

python - 为什么在 python 中使用 sorted 函数进行多级排序会得到错误的答案?

javascript - 如何在给定目标索引数组的情况下对数组进行就地排序?

java - iOS 和 Android 的移动设备唯一性

android - 如何使用 "adb ppp"?

java - API 29 java android 中不推荐使用 getBitmap