Reputation: 301
I have this Firebase database (the list was created by the push()
method):
I want to remove only "Place 1".
I tried the following code, but it deletes the entire "Restaurants" node...
databaseReference.child("Restaurants").orderByValue().equalTo("Place 1")
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
dataSnapshot.getRef().setValue(null);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
How do I remove only "Place 1"?
Upvotes: 2
Views: 882
Reputation: 87
TRY This:
databaseReference.child("Restaurants").orderByValue().equalTo("Place 1")
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
dataSnapshot.getRef().removeValue(); //used this
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Upvotes: 0
Reputation: 2165
You are using databaseReference.child("Restaurant")
as a base reference. Please note that orderByValue()
does not change the reference, only filter the values. In short, dataSnapshot
object produced by onDataChange()
will always point to your reference.
You should do it like this:
... onDataChange(DataSnapshot dataSnapshot) {
// dataSnapshot object will point at "Restaurant"
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
// snapshot object is every child of "Restaurant" that match the filter
snapshot.getRef().setValue(null);
}
}
Hope this helps.
Upvotes: 1