0

My RecyclerView receives data from phpMyAdmin, I added a code to the Adapter so that I can delete element, but when I click on the button, the element is delete on one position higher, that is, I click delete element 5 and element 6 is deleted, how to fix this ?

Adapter Code

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {

    Context context;
    ArrayList<Model> models;

    public MainAdapter(Context context, ArrayList<Model> models) {
        this.context = context;
        this.models = models;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.item_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int number) {

        holder.position.setText(models.get(number).getPosition());
        holder.id.setText(models.get(number).getId());
        holder.name.setText(models.get(number).getName());

        holder.cardview.setOnClickListener(view -> {
            Intent intent = new Intent(context, ActivitySecond.class);
            intent.putExtra("id", models.get(number).getId());
            context.startActivity(intent);
        });

        holder.delete.setOnClickListener(view -> {

            String url = "";
            StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                    response -> Toast.makeText(context, "Success", Toast.LENGTH_SHORT).show(),
                    error -> Toast.makeText(context, "Error: " + error, Toast.LENGTH_SHORT).show()) {
                @Override
                public Map<String, String> getParams() {
                    Map<String, String> params = new HashMap<>();

                    params.put("position", models.get(number).getPosition());

                    return params;
                }
            };

            models.remove(number);
            notifyItemRemoved(number);
            notifyItemRangeChanged(holder.getAdapterPosition(), models.size());

            RequestQueue requestQueue = Volley.newRequestQueue(context);
            requestQueue.add(stringRequest);

        });
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView position, id, name;
        CardView cardview, delete;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            position = itemView.findViewById(R.id.position);
            id = itemView.findViewById(R.id.id);
            name = itemView.findViewById(R.id.name);
            cardview = itemView.findViewById(R.id.cardview);
            delete = itemView.findViewById(R.id.delete);
        }
    }

    public void clear() {
        int size = models.size();
        models.clear();
        notifyItemRangeRemoved(0, size);
    }
}

Initially, I only had this part models.remove(number);

Later I read that if you add holder.getAdapterPosition() then the problem should be fixed, but it didn’t help

  • The problem is somewhere in notifying the Adapter about changes in the data set. If you'll do it with the notifyItemRemoved only, it'll behave as you described. However adding the second line with notifyItemRangeChanged solves it. Are you sure, you pasted not working code? – Mieczkoni Nov 18 '22 at 22:41
  • @Mieczkoni I just checked the code, yes it's not a working code, but I described the problem a little wrong, the problem is that the correct element disappears, but it is deleted one position higher in the table, if you try to delete the last element in the list, it will give an error `Unhandled exception java .lang.IndexOutOfBoundsException: Index: 2, Size: 2` – Michael Nov 18 '22 at 23:14

0 Answers0