[英]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.