I already followed this. But unfortunately, when I delete a data with the SAME value as the other, it gets deleted as well. I want to be deleting a single data only.
Here is my database structure. .
I want to be only deleting only a single value.
Query query = databaseReference.orderByChild("address").equalTo(tvPatientAddress.getText().toString());
query.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
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) {
}
});
I have this code
Any chance you guys have an idea? I think the best way to do this is by getting the ID of the patient and deleting that data, since the ID is unique. Any help appreciated!
Callbacks are removed by calling the off() method on your Firebase database reference. You can remove a single listener by passing it as a parameter to off() .
Deleting Fields For deleting a specific field from a document, use the FieldValue. delete() method when we update a document.
In order to delete multiple entries from your database, you need to know all those locations (refernces). So with other words, in the way you add data, you should also delete it. This method atomically deletes all those entries.
First, get the specific value that you want to delete, then remove it.
If you do not know about the key of that object, you have to query to get the object key from DataSnapshot.
This can be done by using getKey() method
(ex. postsnapshot.getKey()
)
Or, check out my Firebase Helper Classes or Firebase Helper Class Tutorial
mdatabaseReference.child("users").orderByKey().equalTo(uid).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postsnapshot :dataSnapshot.getChildren()) {
String key = postsnapshot.getKey();
dataSnapshot.getRef().removeValue();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With