【问题标题】:How can I retrieve the value from Firebase Realtime database?如何从 Firebase 实时数据库中检索值?
【发布时间】:2022-01-08 04:53:23
【问题描述】:

我有这个结构,我想从该身份验证 ID 中检索所有值,如何一次性从(用户/驱动程序)获取所有这些详细信息?

【问题讨论】:

标签: java database firebase firebase-realtime-database


【解决方案1】:

与我之前的回答相同:Error when i try to update data from firebase

我建议添加额外的 child 让名称为 id 例如,此子项的值将与 firebase 推送 id 的值相同。

让我们开始编辑吧:

为适配器传递一个字符串 textId 变量,这样当您从菜单中单击编辑时,它会将 textId 值设置为来自 firebase 的推送 id,这将是具有您需要编辑的数据的子项。

现在使用child(textId).updateChildren(//Your hashmap);

它会起作用的。

我的用法示例:

String textId= productRef.child(UID).child("images").push().getKey();


HashMap<String ,Object> hashMap = new HashMap<>();
        hashMap.put("image", url);
        hashMap.put("id", textId);

 productRef.child(UID).child("images").child(textId).updateChildren(hashMap);

【讨论】:

    【解决方案2】:
     FirebaseDatabase.getInstance().getReference().child("Users").child("Drivers").addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {
                        for(DataSnapshot snapshot1:snapshot.getChildren()){
                            MyModel myModel=snapshot1.getValue(MyModel.class);
                            list.add(myModel);
                            AdapterSecond  second = new AdapterSecond(list,MainActivity.this);
                            r1.setAdapter(second);
                            r1.setLayoutManager(new LinearLayoutManager(MainActivity.this));
                        }
                    }
    
                    @Override
                    public void onCancelled(@NonNull DatabaseError error) {
    
                    }
                });
    

    我刚刚通过上面的代码解决了我的错误,我希望它可以帮助别人:)

    【讨论】:

    • 使用此解决方案,很可能您最终会在适配器中拥有一个项目,因为您在循环的每次迭代中都创建了 AdapterSecond 类的新实例,对吧?
    • 不,它工作得很好,firebase 实时中的所有细节都显示在我的回收站视图中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-11
    • 1970-01-01
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多