简体   繁体   English

如何从 Android Studio 中的 Firebase 中删除推送键数据

[英]How to delete push key data from Firebase in Android Studio

can I know on how to delete data under UserID push key?我可以知道如何删除 UserID 按键下的数据吗? Currently, I'm using firebase and I need to delete specific booking data under UserID.目前,我正在使用 firebase,我需要删除 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.例如,我想删除整个 MOjF8qthpvBGrbZMtBS 数据,但是一旦我单击删除按钮,所有数据也会被删除。 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这是我的删除 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您的 for 循环将删除每条记录,因为不存在删除特定记录的 if 条件,以便在 for 循环中删除该特定记录

@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.如果您无权访问userId ,那么您需要迭代两次,然后使用getRef().removeValue()您将能够删除带有数据的随机 id。


If you have access to the userid , then just do:如果您有权访问userid ,那么只需执行以下操作:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
FirebaseDatabase.getInstance().getReference().child("Booking").child(user.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {

getCurrentUser() returns the currently logged in user. getCurrentUser()返回当前登录的用户。

You need to reference the userId and then just loop once and it will get delete the data under userId .您需要引用userId ,然后只循环一次,它将删除userId下的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM