我能知道如何删除userid按键下的数据吗?目前,我正在使用firebase,需要删除userid下的特定预订数据。例如,我想删除整个mojf8qthpvbgrbzmtbs数据,但一旦单击delete按钮,所有数据也会被删除。请帮忙;')
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();
}
}
});
}
});}
}
2条答案
按热度按时间jucafojl1#
您需要执行以下操作:
如果你不能访问
userId
,则需要迭代两次,然后使用getRef().removeValue()
您将能够删除随机id及其下的数据。如果你有权访问
userid
,然后只需执行以下操作:vhmi4jdf2#
for循环将删除每个记录,因为不存在删除特定记录的if条件,以便删除for循环中的特定记录
不确定语法,但逻辑在这里