searchview复制数据

sh7euo9m  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(300)

搜索视图在操作栏中实现,搜索工作正常,但例如,如果用户搜索两个字符pa,则我的列表包含两个与pa相关的数据,即pack1、pack2,然后在recycler视图中加载数据。现在,如果用户输入另一个字符l,到前面的字符pa,现在我的搜索视图有pal,但数组列表与搜索不匹配,列表不包含任何pal项。即,现在回收器视图为空。现在,如果我清除字符l,列表将重新加载数据pack1,pack2,但是结果加载了2次,即,在recyclerview中没有显示2个项目,而是总共显示了4个项目。当搜索被清除时,它正在复制项目。
搜索视图

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_order, menu);
        searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
        searchView.setImeOptions(EditorInfo.IME_ACTION_DONE);
        searchView.setMaxWidth(Integer.MAX_VALUE);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                mOrderAdapter.getFilter().filter(query);
                searchView.clearFocus();
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                if(newText.length()==0){
                    getData();
                    searchView.clearFocus();
                }
                mOrderAdapter.getFilter().filter(newText);
                return true ;
            }
        });
        return true;
    }

过滤代码

@Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                String charString = constraint.toString();
                if (charString.isEmpty()||charString.length()==0) {
                    mListFilter = mOrderArrayList;
                } else {
                    List<Order> filteredList = new ArrayList<>();
                    ArrayList<Order> templist=new ArrayList<Order>();
                    for (Order ord : mOrderArrayList) {
                        if (ord.getOs_OrderNo().toLowerCase().contains(charString.toLowerCase())
                                || ord.getItemName().toLowerCase().contains(charString.toLowerCase())) {
                            filteredList.add(ord);
                            templist.addAll(mListFilter);
                        }
                    }
                    mOrderArrayList.removeAll(templist);
                    mOrderArrayList.addAll(filteredList);
                    mListFilter = filteredList;
                }

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

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                mListFilter = (ArrayList<Order>) results.values;
                notifyDataSetChanged();
            }
        };
    }

非常感谢您的帮助。
适配器代码

public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.MyViewHolder> implements Filterable {

    private Context mContext;
    private List<Order> mListFilter,mOrderArrayList;
    private SharedPreferences sharedPreferences;
    private Resources r;

    public OrderAdapter(Context context, ArrayList<Order> orderList) {
        mContext = context;
        mListFilter = orderList;
        mOrderArrayList = orderList;
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
        r = context.getResources();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView orderNoView, itemNameView, qtyView, statusView, companyNameView;
        private Order currentItem;

        MyViewHolder(View view) {
            super(view);
            orderNoView = view.findViewById(R.id.orderNo);
            itemNameView = view.findViewById(R.id.item);
            qtyView = view.findViewById(R.id.qty);
            statusView = view.findViewById(R.id.status);
            companyNameView = view.findViewById(R.id.company);

        }

        void bind (Order om) {  //<--bind method allows the ViewHolder to bind to the data it is displaying
            orderNoView.setText(om.getOs_OrderNo());
            itemNameView.setText(String.valueOf(om.getItemName()));
            qtyView.setText("Qty - " + String.valueOf(om.getOs_Qty()));
            statusView.setText(om.getStatusName());

            if (sharedPreferences.getString(r.getString(R.string.key_CompanyInUser), "").equals("0")){
                companyNameView.setVisibility(View.GONE);
            } else {
                companyNameView.setText(om.getCustomerName());
            }
            currentItem = om; //<-- keep a reference to the current item
        }
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.adapter_order, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        Order item =  mListFilter.get(position);
        holder.bind(item);

    }

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

    @Override

    public int getItemViewType(int position){
        return  position;

    }
@Override
public Filter getFilter() {
    return new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            String charString = constraint.toString();
            List<Order> filteredList = new ArrayList<>();
            if (charString.isEmpty()||charString.length()==0) {
                filteredList.addAll(mOrderArrayList);
            } else {
                for (Order ord : mOrderArrayList) {
                    if (ord.getOs_OrderNo().toLowerCase().contains(charString.toLowerCase())
                            || ord.getItemName().toLowerCase().contains(charString.toLowerCase())) {
                        filteredList.add(ord);
                    }
                }
            }
            FilterResults filterResults = new FilterResults();
            filterResults.values = filteredList;
            return filterResults;
        }
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            mListFilter = (ArrayList<Order>) results.values;
            notifyDataSetChanged();
        }
    };
}

}

单击侦听器代码

brandView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), brandView,
                new RecyclerTouchListener.ClickListener() {
                    @Override
                    public void onClick(View view, int position) {
                        isRefreshData = true;
                        startActivity(new Intent(mContext, ViewOrderDetails.class).putExtra("Key-Intent-OrderNo",mOrderArrayList.get(position).getOs_OrderNo()).putExtra("Key-Intent-BrandName",mOrderArrayList.get(position).getos_BrandName()));
                    }
                    @Override
                    public void onLongClick(View view, int position) {
                    }
                }));
sqyvllje

sqyvllje1#

因为你在用 addAll 在循环中 #performFiltering . 还有为什么你有两个本地列表进行过滤?检查下面的方法,它应该工作。我已经删除了额外的代码和代码,这是导致问题。

@Override
public Filter getFilter() {
    return new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            String charString = constraint.toString();
            List<Order> filteredList = new ArrayList<>();
            if (charString.isEmpty()||charString.length()==0) {
                filteredList.addAll(mOrderArrayList);
            } else {
                for (Order ord : mOrderArrayList) {
                    if (ord.getOs_OrderNo().toLowerCase().contains(charString.toLowerCase())
                            || ord.getItemName().toLowerCase().contains(charString.toLowerCase())) {
                        filteredList.add(ord);
                    }
                }
            }
            FilterResults filterResults = new FilterResults();
            filterResults.values = filteredList;
            return filterResults;
        }
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            mListFilter = (ArrayList<Order>) results.values;
            notifyDataSetChanged();
        }
    };
}

相关问题