作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我可以知道如何删除UserID push key下的数据吗?目前,我正在使用 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());
这是我的删除函数
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/
我是一名优秀的程序员,十分优秀!