简体   繁体   English

如何删除 firebase 数据库中的整个关键数据?

[英]How to delete whole key data in firebase database?

I making an app using Firebase.我使用 Firebase 制作应用程序。 Structure of my data storage is-我的数据存储结构是-

在此处输入图像描述

I am calling subject and description in another activity to show them.我在另一个活动中调用主题和描述来展示它们。 I added a button in that activity which delete that particular key(-L6g7ttOxr... highlighted portion) data shown in Image above.我在该活动中添加了一个按钮,用于删除上图中显示的特定密钥(-L6g7ttOxr ... 突出显示的部分)数据。 I successfully got that key in second activity for particular list item to give database reference.我在特定列表项的第二项活动中成功获得了该密钥以提供数据库参考。

public void deleteCurrentMemo(){
    AlertDialog.Builder alert = new AlertDialog.Builder(
            DisplayMemo.this);
    alert.setTitle("Delete Memo");
    alert.setMessage("Are you sure you want delete current Memo?");
    Toast.makeText(this,key,Toast.LENGTH_SHORT).show();
    alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            mFirebaseDatabase = FirebaseDatabase.getInstance();
            mMemoDatabaseReference = mFirebaseDatabase.getReference().child("memos").child(key);
            mMemoDatabaseReference.removeValue();
            finish();
        }
    });
    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    alert.show();
}

Above code is in second activity called on delete Button.上面的代码是在调用删除按钮的第二个活动中。 Now how can I delete complete key data?现在如何删除完整的关键数据?

You will need to pass the key of the memo to the second activity.您需要将备忘录的密钥传递给第二个活动。

Typically this is quite easy, since you already get the key when you read the data from Firebase.通常这很容易,因为当您从 Firebase 读取数据时,您已经获得了密钥。 Just pass is along with the subject and subscription to the second activity, and then into deleteCurrentMemo .只需传递主题和订阅第二个活动,然后进入deleteCurrentMemo

By this way i have successfully delete child node.通过这种方式,我已经成功删除了子节点。

N:B You must need key of your node that you want to delete. N:B 您必须需要要删除的节点的密钥。

    database.getReference().child("Groups").child(groupItem.getGroupkey()).removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        new CustomMessage(GroupDetailsActivity.this, "Successfully deleted");
                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Customlog.showlogD("ERROR_MSG",e.getMessage());
                    }
                })

;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM