java—在recyclerview中使用微调器筛选自定义arraylist正在清除整个列表

jobtbby3  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(255)

我正在尝试根据项目的直径筛选项目列表。直径位于产品名称的字符串中,该字符串是自定义对象的一个元素。到目前为止,当我从微调器中选择一个直径时,它正在清除列表中的每一项。
微调器逻辑

//Spinner
        diameterSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Object diameterFilter = spinnerAdapter.getItem(position);
                dAdapter.getFilter().filter(diameterFilter.toString());
                dAdapter.notifyDataSetChanged();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

适配器上的筛选器代码

//FILTER
    @Override
    public Filter getFilter() {
        return displayFilter;
    }

    private Filter displayFilter = new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            List<Inventory> filteredList = new ArrayList<>();

            if (constraint == null || constraint.length() == 0){
                filteredList.addAll(displayedListFull);
            }else {
                String filterPattern = constraint.toString().toLowerCase().trim();

                for (Inventory item : displayedListFull){
                    if (item.getProductName().toLowerCase().contains(filterPattern)){
                        filteredList.add(item);
                    }
                }
            }
            FilterResults results = new FilterResults();
            results.values = filteredList;
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            displayedList.clear();
            displayedList.addAll((List)results.values);
            notifyDataSetChanged();
        }
    };
5cnsuln7

5cnsuln71#

改变 getItem 进入 getItemAtPosition 在微调器回调中

diameterSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        Object diameterFilter = spinnerAdapter.getItemAtPosition(position); // here is the change
        dAdapter.getFilter().filter(diameterFilter.toString());
        dAdapter.notifyDataSetChanged();
    }

// rest of your code

相关问题