Uttam Panchasara Uttam Panchasara - 2 months ago 25
Android Question

Remove Specific value From Firebase Database

I want to Delete Specific value from Firebase Database following is Database which i have stored

enter image description here

Now, I want to Delete Following Values

fname: "John"


lname: "wick"


enter image description here

Here, What i have try to delete specific value from Firebase..


DatabaseReference demo = mReference.child("demo").orderByValue().equalTo("John").getRef();

demo.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
dataSnapshot.getRef().removeValue();
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});


But this will delete all values from database...

Question
: How to delete that specific value??? what i have to do for that

Any help will be appreciated.

Answer

Instead of using addListenerForSingleValueEventtry using addChildEventListener as below,

Query queryRef = mReference.child("demo").orderByChild("fname").equalTo("John");

queryRef.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot snapshot, String previousChild) {
      snapshot.getRef().setValue(null);
    }
});
Comments