如何在Android Studio中从Firebase中删除推送密钥数据

3

我可以知道如何删除UserID push key下的数据吗?目前,我正在使用Firebase,并且需要删除UserID下特定的预订数据。例如,我想要删除整个MOjF8qthpvBGrbZMtBS数据,但是一旦我点击删除按钮,所有数据也会被删除。请帮忙 ;')

enter image description here

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

                    });
        }
    });}

}

在 Stack Overflow 上,请不要展示文字和代码的图片。请将文本复制到问题本身中,并进行格式化,以便易于阅读、复制和搜索。您可以使用底部的编辑链接编辑问题以进行更正。 - Doug Stevenson
你需要为代码提供更多关于预订日期或类似信息的细节。 - Ashish
提供 Firebase 树形结构以了解实际问题。 - Kalpesh Kulye
我正在请求活动代码,而不是RecyclerView代码,因为我们还需要获取子项的ID。 - Ashish
请检查我的答案。 - Peter Haddad
显示剩余3条评论
2个回答

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

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


0
你需要做以下事情:
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 下的数据。


先生,如果管理员想要删除数据怎么办?还是一样的方式吗?我在用户端尝试过了,它可以工作。但是当我在管理员端使用它来删除预订数据时,它无法工作。 - Fatimah Azzaharah
1
如果在管理员侧,则管理员是当前登录用户。您需要访问用户ID。如果您无权访问该ID,则必须像我在第一个代码片段中所示那样循环两次。 - Peter Haddad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接