如何从Android中的Firebase删除单个子级? [英] How to delete the individual childs from firebase in android?

查看:89
本文介绍了如何从Android中的Firebase删除单个子级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我浏览了各种链接和解决方案从firebase中删除-child- 如何删除子节点并尝试可以在我的项目中使用,但没有任何效果.按照以下链接 Deletion子节点的数量

I went through various links and solutions delete-child-from firebase, How to remove child nodes and try to use in my project but nothing works.As following the link Deletion of child nodes,

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
rootRef.child("calendario").child("-L7jrJ6DtQWrmZsC4zvT").removeValue();

它适用于没有生成密钥的情况,这不是我的情况.数据库结构在这里.

it works on the case where key is not generated which is not mine case.Database Structure is here.

下面的一段代码用于删除我尝试过的单个孩子. 感谢您的帮助.

Following piece of Code is for deletion of individual child which I have tried. Any help is appreciated.

 delete.setOnClickListener(new View.OnClickListener() {
            @Override
        public void onClick(View view) {


            FirebaseDatabase ref = FirebaseDatabase.getInstance();
            DatabaseReference myRef = ref.getReference("Students");
            String key = myRef.push().getKey();
             myRef.child(key).removeValue(); 
        }
    });

推荐答案

使用push()时,您将生成一个新的随机ID,然后在该键上使用removeValue()时将导致删除该键.

When you use push(), you are generating a new random id, then when you use removeValue() on that key it will lead to deleting that key.

如果要删除数据库中的现有密钥,则需要从数据库中检索它,以便能够与removeValue()一起使用.

If you want to delete an existing key in your database, then you need to retrieve it from the database to be able to use it with removeValue().

这篇关于如何从Android中的Firebase删除单个子级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆