删除的回收器项在删除其他项后再次显示

dpiehjr4  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(214)

我使凝乳活动与我的服务器api集成。
在活动开始时,我从服务器获取数据的arraylist,并在recycler视图中进行如下设置

custmerListAdapter = new CustomerListAdapter(customerListSuitcases, context);
        customerList_recyclerView.setAdapter(custmerListAdapter);


这里有三个选项,只有一个选项:-删除
note:- i 我试着不要每次应用程序连接到服务器时都点击api。看,只有一次热api连接到服务器进行tht任务,我必须重新加载服务器数据的最新数据,所以我没有这样做,我在本地删除的项目,根据用户删除,所以会有一个简单的应用程序,而不是耗时。
所以,假设我有三个项目,如果我删除了3个项目,它成功地删除了,当我尝试删除第二个项目时,它从recyclerview和server删除了,但是3个项目再次出现,对于1个项目,同样的,3没有出现,但是2出现了。
服务器api是正确的,我已经检查,问题是与recycycler视图适配器。
这是适配器

public class CustomerListAdapter extends RecyclerView.Adapter<CustomerListAdapter.ViewHolder> implements Filterable
{
    private ArrayList<ListSuitcase> customerListSuitcases = new ArrayList<>();
    private Context context;
    private OnMoreOptionsClickListener onMoreOptionsClickListener;
    private OnItemClickListener onItemClickListener;
    private TextFilter textFilter;
    private ArrayList<ListSuitcase> mStringFilterList = new ArrayList<>();

    public CustomerListAdapter(ArrayList<ListSuitcase> customerListSuitcases, Context context) {
        this.customerListSuitcases = customerListSuitcases;
        this.context = context;
        this.mStringFilterList = customerListSuitcases;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.customer_list_items , parent , false) , onMoreOptionsClickListener , onItemClickListener);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position)
    {
        holder.customer_name.setText(customerListSuitcases.get(position).FirmName);

        if (customerListSuitcases.get(position).GstNo.equals("URP"))
        {
            holder.tv_urp.setVisibility(View.VISIBLE);
        }else
        {
            holder.tv_urp.setVisibility(View.GONE);
        }

    }

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

    @Override
    public Filter getFilter() {
        if (textFilter == null) {
            textFilter = new TextFilter();
        }
        return textFilter;
    }

    public void deleteCustomer(int position)
    {
        customerListSuitcases.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, customerListSuitcases.size());
    }

    public void addData(ListSuitcase listSuitcase)
    {
        customerListSuitcases.add(listSuitcase);
        notifyDataSetChanged();
    }

    public void editData(int position , ListSuitcase listSuitcase)
    {
        customerListSuitcases.remove(position);
        customerListSuitcases.add(listSuitcase);
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder
    {
        TextView customer_name , tv_urp;
        ImageButton more_options;
        OnMoreOptionsClickListener onMoreOptionsClickListener;
        MaterialCardView root_cardView;
        OnItemClickListener onItemClickListener;

        public ViewHolder(@NonNull View itemView , OnMoreOptionsClickListener onMoreOptionsClickListener , OnItemClickListener onItemClickListener)
        {
            super(itemView);
            this.onMoreOptionsClickListener = onMoreOptionsClickListener;
            this.onItemClickListener = onItemClickListener;
            more_options = itemView.findViewById(R.id.more_options);
            customer_name = itemView.findViewById(R.id.customer_name);
            root_cardView = itemView.findViewById(R.id.root_cardView);
            tv_urp = itemView.findViewById(R.id.tv_urp);

            more_options.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view)
                {
                    ImpMethods.oneTimeClickCountdown(view);
                    if (onMoreOptionsClickListener != null)
                    onMoreOptionsClickListener.OnMoreOptionsClick(getAdapterPosition() , more_options , customerListSuitcases.get(getAdapterPosition()));
                }
            });

            root_cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view)
                {
                    ImpMethods.oneTimeClickCountdown(view);
                    if (onItemClickListener != null)
                    onItemClickListener.OnItemClick(getAdapterPosition() , root_cardView , customerListSuitcases.get(getAdapterPosition()));
                }
            });
        }
    }

    public void updateList(ArrayList<ListSuitcase> listSuitcases)
    {
        customerListSuitcases = listSuitcases;
        notifyDataSetChanged();
    }

    public ArrayList<ListSuitcase> getCustomerListSuitcases() {
        return customerListSuitcases;
    }

    public interface OnMoreOptionsClickListener
    {
        void OnMoreOptionsClick(int position , View v , ListSuitcase customerListSuitcase);
    }

    public void setOnMoreOptionsClickListener(OnMoreOptionsClickListener onMoreOptionsClickListener)
    {
        this.onMoreOptionsClickListener = onMoreOptionsClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener)
    {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener
    {
        void OnItemClick(int position , View v , ListSuitcase customerListSuitcase);
    }

    private class TextFilter extends Filter
    {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            constraint = constraint.toString().toLowerCase();
            FilterResults result = new FilterResults();
            ArrayList<ListSuitcase> f = new ArrayList<>();
            ArrayList<ListSuitcase> p = new ArrayList<>();
            for (ListSuitcase listSuitcase : mStringFilterList)
            {
                p.add(listSuitcase);
            }
            if (constraint != null && constraint.toString().length() > 0) {
                for (int i = 0; i < p.size(); i++) {
                    ListSuitcase listSuitcase = p.get(i);
                    if (listSuitcase.FirmName.toLowerCase().contains(constraint))
                        f.add(listSuitcase);
                }
                result.count = f.size();
                result.values = f;
            } else {
                result.values = p;
                result.count = p.size();
            }
            return result;
        }

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

    }

}

选择deletecustomer方法
我正在努力解决这个问题,但做不到。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题