Remove a specific value from Firebase Database

Kyle Anthony Dizon :

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. Here is my database.

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!

Atif AbbAsi :

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();

 }

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=470765&siteId=1