Home > database >  children is not removing from firebase onclick event is written in onbindviewholder in custom recycl
children is not removing from firebase onclick event is written in onbindviewholder in custom recycl

Time:04-10

interesting thing is that toast written on remove completion is showing but value from firebase is not removing.

here is my UI image..

enter image description here

here is my onbindviewholder code...

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

        holder.getShopname().setText(list.get(position).getShopname());
        holder.getApmt_date().setText(list.get(position).getAppointment_date());
        Log.d("QWWQ","size=" list.get(position).getServices());
        holder.getServices().setText(list.get(position).getServices().substring(0,list.get(holder.getAdapterPosition()).getServices().indexOf('-'))) ;
        holder.getSlot().setText(list.get(position).getSlot());
        holder.getAmount().setText(list.get(position).getAmount());

        holder.getcancelBtn().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FirebaseDatabase.getInstance().getReference("Users").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("appointments").addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {
                        for (DataSnapshot datasnapshot: snapshot.getChildren()
                             ) {
                            if(datasnapshot.child("shop_name").getValue(String.class).equals(list.get(holder.getAdapterPosition()).getShopname())){
                                Log.d("CHHH","Users/" FirebaseAuth.getInstance().getCurrentUser().getUid() "/" datasnapshot.getKey() "/appointment_dates/" list.get(holder.getAdapterPosition()).getAppointment_date());
                                FirebaseDatabase.getInstance().getReference("Users").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child(datasnapshot.getKey()).child("appointment_dates").child(list.get(holder.getAdapterPosition()).getAppointment_date()).removeValue(new DatabaseReference.CompletionListener() {
                                    @Override
                                    public void onComplete(@Nullable DatabaseError error, @NonNull DatabaseReference ref) {
                                        FirebaseDatabase.getInstance().getReference("Shops").child("appointments").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child(list.get(holder.getAdapterPosition()).getAppointment_date()).removeValue(new DatabaseReference.CompletionListener() {
                                            @Override
                                            public void onComplete(@Nullable DatabaseError error, @NonNull DatabaseReference ref) {
                                                Toast.makeText(view.getContext(), "Your Appointment With " list.get(holder.getAdapterPosition()).getShopname() " has been cancelled!!",Toast.LENGTH_SHORT).show();
                                            }
                                        });

                                    }
                                });
                            }
                        }
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError error) {

                    }
                });

            }
        });
    }

Firebase database image...

enter image description here

CodePudding user response:

You can use .setValue(null) instead of .removeValue to delete an item from realtime database.

CodePudding user response:

It helps to not ignore errors that you get back from the APIs that you call.

In the call to removeValue, you'll want to check if the error is not null, and log it:

public void onComplete(@Nullable DatabaseError error, @NonNull DatabaseReference ref) {
    if (error != null) {
        Log.e("Firebase", "Error deleting node", error);
    } else {
        Toast.makeText(view.getContext(), "Your Appointment With " list.get(holder.getAdapterPosition()).getShopname() " has been cancelled!!",Toast.LENGTH_SHORT).show();
    }
}

And in your listener you should never leave onCancelled empty. At its minimum that should be

@Override
public void onCancelled(@NonNull DatabaseError error) {
    throw error.toException();
}

Once you can read the error, you can find out what the root cause of the problem is.

  • Related