繁体   English   中英

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

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

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

在此处输入图像描述

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

这是我的删除 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();
                            }
                        }

                    });
        }
    });}

}

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

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

您需要执行以下操作:

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下的数据。

暂无
暂无

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

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