在“搜索”或“过滤”之后,Recyclerview不是“正在排序”

时间:2019-01-12 21:02:30

标签: java android sorting android-recyclerview

我正在开发一个使用“ Recyclerview”的应用程序,并且已经成功实现了Search(在工具栏中使用SearchView),Filtering(在过滤器输入中使用自定义对话框)和排序(价格从低到高和从高到高)。低)。

现在,我遇到的问题是,无论何时在搜索内容或进行过滤之前对列表进行排序,排序都可以正常工作。但是在搜索内容或进行过滤后,排序不起作用。我进行了很多搜索,但找不到解决方案。

这是与我的问题有关的唯一SO问题,但我无法通过提供的答案来解决我的问题:Recyclerview - Sorting not working after searching an 'Arraylist'

PS::我已删除该代码,仅包含相关代码。

Recyclerview适配器:

public class TripListRecyclerViewAdapter extends RecyclerView.Adapter<TripListRecyclerViewAdapter.ViewHolder> implements Filterable {
    private static final String TAG = TripListRecyclerViewAdapter.class.getSimpleName();

    private Context mContext;
    private ArrayList<Trip> mTrips = new ArrayList<>();
    private ArrayList<Trip> mTripsFiltered = new ArrayList<>();

    public TripListRecyclerViewAdapter(Context mContext, ArrayList<Trip> mTrips) {
        this.mContext = mContext;
        this.mTrips = mTrips;
        this.mTripsFiltered = mTrips;
    }

    @Override
    public int getItemCount() {
        return mTripsFiltered.size();
    }

    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence charSequence) {
                String charString = charSequence.toString();
                if (charString.isEmpty()) {
                    mTripsFiltered = mTrips;
                } else if (charString.contains("Min") && charString.contains("Max")) {
                    String minPrice = charString.substring(3, charString.indexOf("Max"));
                    String maxPrice = charString.substring(charString.indexOf("Max") + 3);

                    ArrayList<Trip> filteredList = new ArrayList<>();
                    for (Trip trip : mTrips) {
                        if (trip.getTrip_rate() >= Integer.parseInt(minPrice) && trip.getTrip_rate() <= Integer.parseInt(maxPrice)) {
                            filteredList.add(trip);
                        }
                    }

                    mTripsFiltered = filteredList;
                } else if (charString.contains("Min")) {
                    String minPrice = charString.substring(3);

                    ArrayList<Trip> filteredList = new ArrayList<>();
                    for (Trip trip : mTrips) {
                        if (trip.getTrip_rate() >= Integer.parseInt(minPrice)) {
                            filteredList.add(trip);
                        }
                    }

                    mTripsFiltered = filteredList;
                } else if (charString.contains("Max")) {
                    String maxPrice = charString.substring(3);

                    ArrayList<Trip> filteredList = new ArrayList<>();
                    for (Trip trip : mTrips) {
                        if (trip.getTrip_rate() <= Integer.parseInt(maxPrice)) {
                            filteredList.add(trip);
                        }
                    }

                    mTripsFiltered = filteredList;
                } else {
                    ArrayList<Trip> filteredList = new ArrayList<>();
                    for (Trip trip : mTrips) {
                        if (trip.getTrip_name().toLowerCase().contains(charString.toLowerCase()) || trip.getTrip_destination().toLowerCase().contains(charString.toLowerCase())) {
                            filteredList.add(trip);
                        }
                    }

                    mTripsFiltered = filteredList;
                }

                FilterResults filterResults = new FilterResults();
                filterResults.values = mTripsFiltered;
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
                mTripsFiltered = (ArrayList<Trip>) filterResults.values;
                notifyDataSetChanged();
            }
        };
    }
}

排序模型:

public class Trip implements Serializable {

    public static final Comparator<Trip> PRICE_LOW_TO_HIGH = new Comparator<Trip>() {
        @Override
        public int compare(Trip tripOne, Trip tripTwo) {
            Float tripRateOne = new Float(tripOne.trip_rate);
            Float tripRateTwo = new Float(tripTwo.trip_rate);
            return tripRateOne.compareTo(tripRateTwo);
        }
    };
    public static final Comparator<Trip> PRICE_HIGH_TO_LOW = new Comparator<Trip>() {
        @Override
        public int compare(Trip tripOne, Trip tripTwo) {
            Float tripRateOne = new Float(tripOne.trip_rate);
            Float tripRateTwo = new Float(tripTwo.trip_rate);
            return tripRateTwo.compareTo(tripRateOne);
        }
    };
    public static final Comparator<Trip> DATE_NEW_TO_OLD = new Comparator<Trip>() {
        @Override
        public int compare(Trip tripOne, Trip tripTwo) {
            Integer tripRateOne = new Integer(tripOne.trip_id);
            Integer tripRateTwo = new Integer(tripTwo.trip_id);
            return tripRateTwo.compareTo(tripRateOne);
        }
    };
    @SerializedName("booking_id")
    @Expose
    private int booking_id;
    @SerializedName("trip_rate")
    @Expose
    private float trip_rate;

    public Trip(int booking_id) {
        this.booking_id = booking_id;
    }

    public int getBooking_id() {
        return booking_id;
    }

    public void setBooking_id(int booking_id) {
        this.booking_id = booking_id;
    }
}

Recyclerview的活动

mSortingTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Creating A Popup Menu
                PopupMenu popupMenu = new PopupMenu(TripsActivity.this, mSortingTextView);
                // Inflating Menu From XML Resource
                popupMenu.inflate(R.menu.trip_sorting_menu);
                // Adding Click Listener
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        switch (item.getItemId()) {
                            case R.id.latest:
                                // Handle Latest Click
                                mSortingTextView.setText("Latest");
                                if (mTrips.size() > 0) {
                                    Collections.sort(mTrips, Trip.DATE_NEW_TO_OLD);
                                }
                                mTripListRecyclerViewAdapter.notifyDataSetChanged();
                                return true;
                            case R.id.price_low_to_high:
                                // Handle Price (Low to High) Click
                                mSortingTextView.setText("Price (Low to High)");
                                if (mTrips.size() > 0) {
                                    Collections.sort(mTrips, Trip.PRICE_LOW_TO_HIGH);
                                }
                                mTripListRecyclerViewAdapter.notifyDataSetChanged();
                                return true;
                            case R.id.price_high_to_low:
                                // Handle Price (High to Low) Click
                                mSortingTextView.setText("Price (High to Low)");
                                if (mTrips.size() > 0) {
                                    Collections.sort(mTrips, Trip.PRICE_HIGH_TO_LOW);
                                }
                                mTripListRecyclerViewAdapter.notifyDataSetChanged();
                                return true;
                            case R.id.date_new_to_old:
                                // Handle Date (New to Old) Click
                                mSortingTextView.setText("Date (New to Old)");
                                if (mTrips.size() > 0) {
                                    Collections.sort(mTrips, Trip.DATE_NEW_TO_OLD);
                                }
                                mTripListRecyclerViewAdapter.notifyDataSetChanged();
                                return true;
                            default:
                                return false;
                        }
                    }
                });
                // Displaying The Popup Menu
                popupMenu.show();
            }
        });
        mFilterTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(TripsActivity.this);
                dialogBuilder.setTitle("Filters");
                dialogBuilder.setCancelable(false);
                LayoutInflater layoutInflater = TripsActivity.this.getLayoutInflater();
                View dialogView = layoutInflater.inflate(R.layout.filter_dialog, null);
                dialogBuilder.setView(dialogView);
                TextInputLayout minPriceTextInputLayout = (TextInputLayout) dialogView.findViewById(R.id.minimum_price_text_input_layout);
                TextInputLayout maxPriceTextInputLayout = (TextInputLayout) dialogView.findViewById(R.id.maximum_price_text_input_layout);
                dialogBuilder.setPositiveButton("DONE", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
                dialogBuilder.setNegativeButton("RESET", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        String reset = "";
                        if (mTripListRecyclerViewAdapter != null) {
                            mTripListRecyclerViewAdapter.getFilter().filter(reset);
                        }
                    }
                });
                AlertDialog alertDialog = dialogBuilder.create();
                alertDialog.show();
                alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String minPrice = minPriceTextInputLayout.getEditText().getText().toString().trim();
                        String maxPrice = maxPriceTextInputLayout.getEditText().getText().toString().trim();
                        if (minPrice.equals("") && maxPrice.equals("")) {
                            alertDialog.dismiss();
                        } else {
                            String price = "";
                            if (minPrice.length() > 0 && maxPrice.length() > 0) {
                                if (Integer.parseInt(minPrice) > Integer.parseInt(maxPrice)) {
                                    minPriceTextInputLayout.setError("Minimum Price should be less than Maximum Price");
                                } else {
                                    price = "Min" + minPrice + "Max" + maxPrice;
                                    alertDialog.dismiss();
                                }
                            } else if (minPrice.length() > 0) {
                                price = "Min" + minPrice;
                                alertDialog.dismiss();
                            } else if (maxPrice.length() > 0) {
                                price = "Max" + maxPrice;
                                alertDialog.dismiss();
                            }

                            if (mTripListRecyclerViewAdapter != null) {
                                mTripListRecyclerViewAdapter.getFilter().filter(price);
                            }
                        }
                    }
                });
            }
        });

1 个答案:

答案 0 :(得分:0)

我通过对library IEEE; use IEEE.STD_LOGIC_1164.ALL; entity tb_Divider is -- Port ( ); end tb_Divider; architecture Behavioral of tb_Divider is signal clk, reset, start, ready, done: STD_LOGIC; signal dvsr, dvnd: STD_LOGIC_VECTOR(7 downto 0); signal quo, rmd: STD_LOGIC_VECTOR(7 downto 0); component Divider is port( clk, reset : in STD_LOGIC; start : in STD_LOGIC; dvsr, dvnd : in STD_LOGIC_VECTOR (7 downto 0); ready, done_tick : out STD_LOGIC; quo, rmd : out STD_LOGIC_VECTOR (7 downto 0)); end component Divider; begin UUT: Divider port map( clk => clk, reset => reset, start => start, dvsr => dvsr, dvnd => dvnd, ready => ready, done_tick => done, quo => quo, rmd => rmd); process begin clk <= '0'; wait for 10 ns; clk <= '1'; wait for 10 ns; end process; process begin start <= '0'; dvnd <= x"00"; dvsr <= x"00"; wait for 100 ns; start <= '1'; dvnd <= x"C8"; dvsr <= x"0A"; wait for 10 us; end process; end Behavioral; 原始和过滤后的文本进行排序解决了上述问题。

这是我所做的更改:

Recyclerview适配器:

ArrayList

Recyclerview的活动

public ArrayList<Trip> mTripsFiltered = new ArrayList<>();