如何在android studio中从firebase删除按键数据

a9wyjsp7  于 2021-06-29  发布在  Java
关注(0)|答案(2)|浏览(272)

我能知道如何删除userid按键下的数据吗?目前,我正在使用firebase,需要删除userid下的特定预订数据。例如,我想删除整个mojf8qthpvbgrbzmtbs数据,但一旦单击delete按钮,所有数据也会被删除。请帮忙;')

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());

这是我的删除功能

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();
                            }
                        }

                    });
        }
    });}

}
jucafojl

jucafojl1#

您需要执行以下操作:

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();
             }
         }
        });
        }
    });}

}

如果你不能访问 userId ,则需要迭代两次,然后使用 getRef().removeValue() 您将能够删除随机id及其下的数据。
如果你有权访问 userid ,然后只需执行以下操作:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
FirebaseDatabase.getInstance().getReference().child("Booking").child(user.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
``` `getCurrentUser()` 返回当前登录的用户。
你需要参考 `userId` 然后只要循环一次,就可以删除下面的数据 `userId` .
vhmi4jdf

vhmi4jdf2#

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();
                    }
                }
          }   
     }
}

不确定语法,但逻辑在这里

相关问题