Reputation: 79
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){
}
Upvotes: 0
Views: 188
Reputation: 600006
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();
}
}
Upvotes: 2