gpt4 book ai didi

java - 如何在 Android Studio 中从 Firebase 中删除推送 key 数据

转载 作者:行者123 更新时间:2023-12-04 03:44:54 25 4
gpt4 key购买 nike

我可以知道如何删除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();
}
}

});
}
});}

}

最佳答案

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

不确定语法但逻辑在这里

关于java - 如何在 Android Studio 中从 Firebase 中删除推送 key 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65335499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com