简体   繁体   English

从firebase android中删除自动关键节点

[英]Deleting auto key node from firebase android

I'm not sure if I'm doing this right but I'm creating a Query to get the auto-generated key that is stored in firebase.我不确定我这样做是否正确,但我正在创建一个查询来获取存储在 firebase 中的自动生成的密钥。 I want to delete this key from firebase.我想从 firebase 中删除这个键。 I'm wondering if this the right process?我想知道这是否是正确的过程? Like is it possible to just call removeValue() on the query or have to use Datasnapshot?就像可以只在查询上调用removeValue()或必须使用 Datasnapshot 一样吗? My code is as follows.我的代码如下。 All it does is gets the auto-generated key and prints it.它所做的就是获取自动生成的密钥并打印出来。

DatabaseReference ref = FirebaseDatabase.getInstance().getReference(); String removeQuery = ref.child("EventData").push().getKey(); System.out.println(removeQuery); Log.d(TAG,"Remove Query was called !!!!!!!!!!!!");

Updated1更新1 在此处输入图片说明

Updated2更新2

Is there an easy way to set removeQuery to null?有没有一种简单的方法可以将removeQuery设置为 null?

Here:这里:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference(); 
String removeQuery = ref.child("EventData").push().getKey();

You are creating a new key and storing it inside a variable.您正在创建一个新密钥并将其存储在变量中。 You can do that in the beginning (when storing data to the database), add data to Intent :您可以在开始时执行此操作(将数据存储到数据库时),将数据添加到Intent

DatabaseReference ref = FirebaseDatabase.getInstance().getReference(); 
String removeQuery = ref.child("EventData").push().getKey();
Intent intent = new Intent(getBaseContext(), Activity.class);
intent.putExtra("key", removeQuery);
startActivity(intent);

then later in the other activity you can delete like this:然后在其他活动中,您可以像这样删除:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference(); 
String key = getIntent().getStringExtra("key");
ref.child("EventData").child(key).removeValue();

You have to store the pushID which is M1VA... .您必须存储M1VA... and then you can delete by like this.然后你可以像这样删除。 Refer to this link: https://firebase.google.com/docs/database/android/read-and-write#add_a_completion_callback请参阅此链接: https : //firebase.google.com/docs/database/android/read-and-write#add_a_completion_callback

mDatabase.child("EventData").child("M1VA....").removeValue()
        .addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                // Write was successful!
                // ...
            }
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                // Write failed
                // ...
            }
        });

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

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