java - 如何在 Android Studio 中从 Firebase 中删除推送密钥数据
问题描述
我可以知道如何删除 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());
这是我的删除功能
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();
}
}
}
}
}
不确定语法,但逻辑在这里
推荐阅读
- operating-system - 操作系统问题 Kernel XV6 中断和定时器中断
- excel - 过滤一列并计算另一列中的值
- javascript - ctrl + p vs window.print() 为什么它在设计中表现出尊重?
- c# - 如何访问 C# 面板中动态添加的用户控件的控件?
- javascript - Javascript - 条件语句的检查顺序是什么
- javascript - 通过javascript在组织结构图中绘制垂直节点
- ios - IOS-Swift、CarbonKit回击时,图标还在pager上停留了一段时间,如何让它们立即消失
- c - 是否有 C 的静态代码检查器可以标记缺少互斥锁解锁?
- r - R ggplot 在图形上显示文本以响应特定输入
- c# - 如何返回任务
>?