问题
I want to delete only the 1st child (L7jrJ6DtQWrmZsC4zvT) from Firebase database with an option in an Android app. I searched several places and could not find it. You only have one option to delete a whole database. Can anyone help?
回答1:
If i understood you correctly, you have the key of the child you want to remove and this child has no parent. so you can just use the single event listener like this -
val ref = FirebaseDatabase.getInstance().reference
val applesQuery = ref.child("L7jrJ6DtQWrmZsC4zvT")
applesQuery.addListenerForSingleValueEvent(
object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
dataSnapshot.ref.removeValue()
}
override fun onCancelled(databaseError: DatabaseError) {
Log.e("frgrgr", "onCancelled", databaseError.toException())
}
})
this should work, let me know otherwise.
回答2:
To delete a single object you can use removeValue() method directly on the reference like this:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
rootRef.child("calendario").child("-L7jrJ6DtQWrmZsC4zvT").removeValue();
来源:https://stackoverflow.com/questions/49361440/how-to-delete-a-specific-child-from-firebase-java