Firebase节点删除无法运行Android

时间:2017-03-21 18:51:11

标签: android firebase firebase-realtime-database

我有一个for循环的dataSnaphots像这样:

for (final DataSnapshot advertiser : dataSnapshot.getChildren()) {

我有一个名为' delete'的按钮。必须删除我的数据库中的节点(代码放在for循环中)

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

toast确认删除正在出现,但节点仍然保留在我的数据库中

这是我的数据库:

enter image description here

1 个答案:

答案 0 :(得分:1)

如果您将databaseReference声明为FirebaseDatabase.getInstance().getReference(),那么它将指向您数据库的根数据。从你的代码中,我看到了:

databaseReference.child(advertiser.getKey()).removeValue();

这会删除your-database-root/(advertiserkey)上的值。但是从您的数据库结构截图中,我意识到您要删除your-database-root\Advertiser\(advertiserkey)上的数据。所以你的代码应该是这样的:

databaseReference.child("Advertiser").child(advertiser.getKey()).removeValue();

应该这样做