0

What is the appropriate code to delete records by Specific value from android studio .
Required: Delete all records If starCount field equal 0.

private void deleteData(String strTitle){


}

enter image description here

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
TARIQ ALEED
  • 69
  • 3
  • 11

1 Answers1

2

In order to delete a node you need to know its full path. This means you you'll need to run a query to find the node with starCount equal to 0 and then delete each of them individually.

Something like this:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference("posts");
Query query = ref.orderByChild("starCount").equalTo(0);
query.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
            postSnapshot.getRef().removeValue();
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        throw databaseError.toException();
    }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thank you very much Frank van Puffelen 100%. How to update the (title field) condition (starCount =0 and body="d1") – TARIQ ALEED Aug 20 '19 at 04:42
  • StackOverflow is not a site which does your homework. Please do some research on Firebase Database – Zun Aug 22 '19 at 07:06