1

I have a RecyclerView. One block contains lines and buttons from multiple retrofit responses. The block has an ImageView when you click on it, the block should be deleted. But it removes not the blocks on which it is located, but by the way, they were added to the database. How to bind this button to the block in which it is located. They all have a link to a personal number.

This is the image button method:

    public void DeletePn() {
        DeletePnRequest deletePnRequest = new DeletePnRequest();
        deletePnRequest.setPnumber(this.pnumber.trim());
        Call<DeletePnResponse> deletePnResponseCall = ApiClient.getUserService().delPn(deletePnRequest, token);
        deletePnResponseCall.enqueue(new Callback<DeletePnResponse>() {
            @Override
            public void onResponse(Call<DeletePnResponse> call, Response<DeletePnResponse> response) {
                if(response.isSuccessful()){
                    DeletePnResponse deletePnResponse = response.body();
                    assert deletePnResponse != null;
                    msg = deletePnResponse.getMessage();
                    Toast.makeText(PersonalNumberActivity.this, msg, Toast.LENGTH_LONG).show();
                    recreate();
                } else {
                    Toast.makeText(PersonalNumberActivity.this, "Error " + response.code(), Toast.LENGTH_LONG).show();
                }
            }
        
            @Override
            public void onFailure(Call<DeletePnResponse> call, Throwable t) {
        
            }
        });
    }

This is the RecyclerView adapter

        private List<PersNumResponse> persNumResponseList;
        private Context context;
        
        public void setData(List<PersNumResponse> persNumResponseList) {
            this.persNumResponseList = persNumResponseList;
            notifyDataSetChanged();
        }
    
        @NonNull
        @Override
        public AdapterVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            context = parent.getContext();
            return new Adapter.AdapterVH(LayoutInflater.from(context).inflate(R.layout.recycler_view_row, parent, false));
        }
    
        @Override
        public void onBindViewHolder(@NonNull AdapterPn4PayVH holder, int position) {
            PersNumResponse persPayResponse = persNumResponseList.get(position);

    
            String pnumber = persNumResponse.getPnumber();
            String city = persNumResponse.getCity();
            String street = persNumResponse.getStreet();
            String house = persNumResponse.getHouse();
    
            holder.pnumber.setText(pnumber);
            holder.city.setText(city);
            holder.street.setText(street);
            holder.house.setText(house);
        }
    
        @Override
        public int getItemCount() {
            return persNumResponseList.size();
        }
    
        public class AdapterVH extends RecyclerView.ViewHolder {
    
            TextView pnumber;
            TextView city;
            TextView street;
            TextView house;
    
            public AdapterVH(@NonNull View itemView) {
                super(itemView);
                pnumber = itemView.findViewById(R.id.pnumber);
                city = itemView.findViewById(R.id.city);
                street= itemView.findViewById(R.id.street);
                house = itemView.findViewById(R.id.house);
    //this button
                del_pn_btn = itemView.findViewById(R.id.del_pn_btn);
    
                del_pn_btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(context instanceof PersonalNumberActivity){
                            ((PersonalNumberActivity)context).DeletePn();
                        }
                    }
                });
            }
        }

1 Answer 1

0

Try to add the pnumber value as a parameter of your DeletePn method and move the initialization of your Button setOnClickListener in the onBindViewHolder method:

    public void DeletePn(String pnumber) {
        ...
        deletePnRequest.setPnumber(pnumber.trim());
        ...
    }

Recycler view adapter

        @Override
        public void onBindViewHolder(@NonNull AdapterPn4PayVH holder, int position) {
            PersNumResponse persPayResponse = persNumResponseList.get(position);

            String pnumber = persNumResponse.getPnumber();
            String city = persNumResponse.getCity();
            String street = persNumResponse.getStreet();
            String house = persNumResponse.getHouse();
    
            holder.pnumber.setText(pnumber);
            holder.city.setText(city);
            holder.street.setText(street);
            holder.house.setText(house);

            holder.del_pn_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(context instanceof PersonalNumberActivity){
                        ((PersonalNumberActivity)context).DeletePn(pnumber);
                    }
                }
            });
        }
    
        ...
    
        public class AdapterVH extends RecyclerView.ViewHolder {
    
            TextView pnumber;
            TextView city;
            TextView street;
            TextView house;
            Button del_pn_btn;
    
            public AdapterVH(@NonNull View itemView) {
                super(itemView);
                pnumber = itemView.findViewById(R.id.pnumber);
                city = itemView.findViewById(R.id.city);
                street= itemView.findViewById(R.id.street);
                house = itemView.findViewById(R.id.house);
                del_pn_btn = itemView.findViewById(R.id.del_pn_btn);
            }
        }
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.