can I know on how to delete data under UserID push key? Currently, I'm using firebase and I need to delete specific booking data under UserID. For example, I want to delete the whole MOjF8qthpvBGrbZMtBS data, but once I click on delete button, all of the data is deleted as well. Please help;')
public class BookingAdapterRecyclerView extends
RecyclerView.Adapter<BookingAdapterRecyclerView.MyViewHolder>{
private List<Booking>bookingList;
private Activity mActivity;
public class MyViewHolder extends RecyclerView.ViewHolder{
public LinearLayout rl_layout;
public TextView
tvname,tvphone,tvhouse,tvdate,tvtime,tvstatus,tvremarks;
public RadioGroup radioGroup;
public Button bDelete, bVerify;
public MyViewHolder (View view){
super(view);
rl_layout = view.findViewById(R.id.rl_layout);
tvname = view.findViewById(R.id.tv_name);
tvphone = view.findViewById(R.id.tv_phone);
tvhouse = view.findViewById(R.id.tv_house);
tvdate = view.findViewById(R.id.tv_date);
tvtime = view.findViewById(R.id.tv_time);
tvstatus = view.findViewById(R.id.tv_status);
tvremarks = view.findViewById(R.id.tv_remarks);
bDelete = view.findViewById(R.id.delete_item);
bVerify = view.findViewById(R.id.btn_verify);
}
}
public BookingAdapterRecyclerView(List<Booking>bookingList,Activity
activity){
this.bookingList = bookingList;
this.mActivity = activity;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.bookinglist_item, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
final Booking book = bookingList.get(position);
holder.tvname.setText("Name :" + book.getName());
holder.tvphone.setText("Phone Number :"+book.getPhone());
holder.tvhouse.setText("House Address :"+book.getHouse());
holder.tvdate.setText("Date Booking :"+book.getbDate());
holder.tvtime.setText("Time Booking"+book.getbTime());
This is my delete function
holder.bDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FirebaseDatabase.getInstance().getReference()
.child("Booking")
.addListenerForSingleValueEvent(new
ValueEventListener() {
@Override
public void onDataChange(DataSnapshot
dataSnapshot) {
for (DataSnapshot userSnapshot :
dataSnapshot.getChildren()){
book.getKey();
userSnapshot.getRef().removeValue();
}
}
});
}
});}
}
Your for loop will remove every record as there is no if condition present to remove particular record, In order to remove that specific record inside for loop
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) // userSnapshot contains userId
{
if (userSnapshot == userId) // check userId credential
{
for (DataSnapshot bookSnapshot : userSnapshot.getChildren()) // it will fetch every book record in db
{
if(bookSnapshot.getChildren() == "MOjF8qthpvBGrbZMtBS")
{
userSnapshot.getChildren().removeValue();
}
}
}
}
}
Not sure about the syntax but logic is here
You need to do the following:
FirebaseDatabase.getInstance().getReference().child("Booking").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot userSnapshot : dataSnapshot.getChildren()){
for(DataSnapshot subSnapshot : userSnapshot.getChildren()){
subSnapshot.getRef().removeValue();
}
}
});
}
});}
}
If you don't have access to the userId
, then you need to iterate twice and then using getRef().removeValue()
you will be able to delete the random id with the data under it.
If you have access to the userid
, then just do:
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
FirebaseDatabase.getInstance().getReference().child("Booking").child(user.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
getCurrentUser()
returns the currently logged in user.
You need to reference the userId
and then just loop once and it will get delete the data under userId
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.