当孩子从表中嵌套时,如何在Firebase中获取数据 [英] how to fetch data in firebase when the child is nested from a table

查看:59
本文介绍了当孩子从表中嵌套时,如何在Firebase中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上面的图像是我在firebase中的数据库结构.我不知道如何获取子Latlng中的数据.我将如何获取?

The above image is my database structure in firebase. I don't know how to fetch the data inside the child Latlng. How would I fetch it?

推荐答案

在您的项目中,您需要具有一个代表驱动程序"实体的POJO.像这样:

In your project, your need to have a POJO representing the "Driver" entity. Something like:

public class Driver {

    private String age;
    private String busNum;
    // etc ...

    private HashMap<String, Object> Latlng;

    public Driver() {
    }

    // getters ...

    public HashMap<String, Object> getLatlng() {
        return Latlng;
    }

}

Firebase中,必须声明内部对象具有HashMaps<String, Object>,其中String是键,而Object可以是模型中的任何其他对象(在您的情况下,Float我是d打赌).

In Firebase, inner objects have to be declared has HashMaps<String, Object>, where the String is the key and the Object can be any other object in your model (in your case a Float I'd bet).

现在,如果您想只一次读取某个值 ,只需调用addListenerForSingleValueEvent方法,如下所示:

Now, if you want to read some value only once, simply call addListenerForSingleValueEvent method, like this:

FirebaseDatabase.getInstance()
                .getReferenceFromUrl(URL_DRIVERS)
                .child(driverKey)
                .addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        Driver driver = dataSnapshot.getValue(Driver.class);

                        // And here you can access Latlng object
                        latitude = driver.getLatlng().get("latitude");
                        longitude = driver.getLatlng().get("longitude");
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                        // handle error ...
                    }
                });

注意:URL_DRIVERS是您的根节点url( https://your-project.firebaseio.com/)加上您的驱动程序节点位置(在您的情况下为驱动程序")

Note: URL_DRIVERS would be your root node url (https://your-project.firebaseio.com/) plus your Driver node location (in your case "Driver")

这篇关于当孩子从表中嵌套时,如何在Firebase中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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