Reputation: 61
I faced an issue when I had to delete that data which user entered in here this photo. If the user entered enrollment number 60
then I have to delete all data from marks where the enrollment number is 60
.
It deletes the data which user entered and it verifies that enrollment from marks and delete all data where enrollment is 66
it means delete that document or node.
I tried the following that did not work.
reff.child("Marks").removeValue().equals(et1);
Upvotes: 1
Views: 2423
Reputation: 61
String et1 =e1.getText().toString().trim();
reff.child("Marks").orderByChild("enrollment").equalTo(et1)
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
{
for (DataSnapshot ds : dataSnapshot.getChildren())
{
ds.getRef().removeValue();
Toast.makeText(getApplicationContext(), "Deleted", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
{
Toast.makeText(getApplicationContext(), "Failed to delete", Toast.LENGTH_SHORT).show();
}
});
Upvotes: 0
Reputation: 1210
Try this code.
int enrollmentId = Integer.parseInt( et1.getText().toString() );
reff.child("Marks").orderByChild("enrollment").equalTo(enrollmentId)
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
{
for (DataSnapshot ds : dataSnapshot.getChildren())
{
ds.getRef().removeValue();
Toast.makeText(context, "Deleted", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
{
Toast.makeText(context, "Failed to delete", Toast.LENGTH_SHORT).show();
}
});
Upvotes: 1
Reputation: 80914
To delete data do the following:
DatabaseReference data = FirebaseDatabase.getInstance().getReference().child("Marks");
data.orderByChild("enrollment").equalTo("66").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot data: dataSnapshot.getChildren()){
data.getRef().removeValue();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Add a reference to child Marks
then using the query orderByChild
you can get the correct node and delete it
Upvotes: 1