Reputation: 711
I have a for loop of dataSnaphots like this:
for (final DataSnapshot advertiser : dataSnapshot.getChildren()) {
and I have a button called 'delete' which has to delete a node in my database (code placed inside the for loop)
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(getActivity())
.setTitle("Delete service")
.setMessage("Do you want to delete this service?")
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
databaseReference.child(advertiser.getKey()).removeValue()
.addOnSuccessListener(new OnSuccessListener<Void>() {
ProgressDialog progressDialog = ProgressDialog.show(getActivity(), "", "Deleting service...", true);
@Override
public void onSuccess(Void aVoid) {
progressDialog.dismiss();
Toast.makeText(getActivity(), "Service deleted", Toast.LENGTH_LONG).show();
}
});
}
}).setNegativeButton(android.R.string.no, null).show();
}
});
the toast to confirm the deletion is appearing but the node still remains in my database
here's my database:
Upvotes: 0
Views: 66
Reputation: 2165
If you declare databaseReference
as FirebaseDatabase.getInstance().getReference()
, then it will point to root data of your database. And from your code, I see:
databaseReference.child(advertiser.getKey()).removeValue();
That will remove value on your-database-root/(advertiserkey)
. But from your database structure screenshot, I realize you want to delete data on your-database-root\Advertiser\(advertiserkey)
. So your code should look like this:
databaseReference.child("Advertiser").child(advertiser.getKey()).removeValue();
And that should do
Upvotes: 1