简体   繁体   English

如何从 Firebase 实时数据库的推送方法中删除自动生成的项目

[英]how to delete the auto generated item from push method from Firebase Real-time database

I am new to Firebase and I am facing a problem in delete an item from the recycler view I created.我是 Firebase 的新手,在从我创建的回收站视图中删除项目时遇到了问题。 but I am unable to understand how to delete items using an auto-generated key.但我无法理解如何使用自动生成的密钥删除项目。 This is how I added item into the database :这就是我将项目添加到数据库的方式

 DatabaseReference reference = FirebaseDatabase.getInstance().getReference("categories");
     String key = reference.push().getKey();
    reference.child(key).setValue(categoryData).addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {
            if(task.isSuccessful()){

                Toast.makeText(add_category.this, "Category added", Toast.LENGTH_SHORT).show();
                finish();

            }
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Toast.makeText(add_category.this, "Failed to add", Toast.LENGTH_SHORT).show();
        }
    });

As in your comment, if you want to delete, for example, the second item, please use the following lines of code:如您的评论,如果您想删除,例如,第二项,请使用以下代码行:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference secondChildRef = rootRef.child("categories").child("-M1qUEGi0u4rX5Ju59r4");
secondChildRef.removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {
        if (task.isSuccessful()) {
            Log.d("TAG", "Second item deleted!");
        }
    }
});

So to delete a particular item, you to know its ID so you can add it to the reference.因此,要删除特定项目,您需要知道其 ID,以便将其添加到引用中。

暂无
暂无

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

相关问题 创建另一个相同ID的节点后如何从firebase实时数据库中删除一个节点 - How to delete a node from the firebase real-time database after creating another node with same IDs 在 Firebase 上显示来自实时数据库的数据 - Displaying data from Real-time database on Firebase 如何从Firebase实时数据库中具有不同键的节点读取相同的值? - How to read same value from nodes with different keys in Firebase real-time database? 如何从实时数据库中删除填充有对象的Listview中的重复项 - How to delete duplicated items in Listview filled with objects from real-time database 从 Firebase 读取实时数据 - Read real-time data from Firebase 如何使用NodeJS中的Firebase云功能从Firebase实时数据库中找出最近的位置LatLng - How to find out nearest location LatLng from firebase real-time database using firebase cloud function in NodeJS 如何将列表视图中我从 Firebase 实时数据库中调用的数据移动到 android 工作室中的另一个活动? - How to move the data that I have called from the Firebase real-time database, in a listview to another activity in android studio? 如何在firebase实时数据库中保存图像url? - How to save image url in firebase real-time database? 适用于 Android 的 Firebase 实时(在线)数据库的安全性如何? - How Secure is Firebase Real-time (Online) database for Android? 我无法在 recyclerView 上显示来自我的 firebase 实时数据库的数据 - I can't show data from my firebase real-time database on a recyclerView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM