Reputation: 5865
I want to Delete Specific value from Firebase Database following is Database which i have stored
Now, I want to Delete Following Values
fname: "John"
lname: "wick"
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.
Upvotes: 2
Views: 6922
Reputation: 2305
Try this code. Hopefully this will help you Thanks
database.getReference().child("Groups").child(groupItem.getGroupkey()).child("members").child(groupMember.getKey()).removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
new CustomMessage(GroupDetailsActivity.this, "Successfully deleted");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Customlog.showlogD("ERROR_MSG",e.getMessage());
}
});
Database structure: https://i.sstatic.net/Lw3Ia.png
Upvotes: 0
Reputation: 384
hi try using this code,
mReference.child("demo").orderByChild("fname").equalTo("John").addListenerForSingleValueEvent(
new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
mReference.child("demo").child(dataSnapshot.getKey()).setValue(null);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w("TodoApp", "getUser:onCancelled", databaseError.toException());
}
});
Upvotes: 1
Reputation: 5865
Well Thanks to all, After Modification with lots of answer and query.. This is what worked with me.
mReference.child("demo").orderByChild("fname").equalTo("John").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
mReference.child("demo").child(dataSnapshot.getKey()).setValue(null);
//also work
//dataSnapshot.getRef().setValue(null);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Upvotes: 0
Reputation: 67286
Instead of using addListenerForSingleValueEvent
try 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);
}
});
Upvotes: 3
Reputation: 2954
You can try it:
mReference.child("demo").orderByChild("fname").equalTo("John").addListenerForSingleValueEvent(
new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
dataSnapshot.getRef().setValue(null);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w("TodoApp", "getUser:onCancelled", databaseError.toException());
}
});
Upvotes: 0