简体   繁体   English

如何从Firebase检索孩子的所有价值?

[英]How to retrieve all value of child from Firebase?

I have this database in Firebase, this my database structure 我在Firebase中有这个数据库,这是我的数据库结构

My Database.json 我的Database.json

My firebase structure.png 我的firebase structure.png

How do I get all of that value index, this is my code : 我如何获得所有的价值指数,这是我的代码:

ref.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        Map<String, Object> td = (HashMap<String,Object>) dataSnapshot.getValue();

        List<Object> values = new ArrayList<Object>(td.values());
        Log.d("values ", String.valueOf(values));
        Iterator<Object> iterator = values.iterator();
        int length = (int) dataSnapshot.getChildrenCount();
        String[] sampleString = new String[length];
        while(i < length) {
            sampleString[i] = iterator.next().toString();
            Log.d(Integer.toString(i), sampleString[i]);
            i++;
        }
    }

That code just return index from the first values : 该代码只是从第一个值返回索引:

11-17 10:31:07.516 9032-9032/com.arf.tanamanku D/values: [112.788683, -7.281677, 4.983527183532715, 32.00, 1008, 62]
11-17 10:31:07.516 9032-9032/com.arf.tanamanku D/0: 112.788683
11-17 10:31:07.516 9032-9032/com.arf.tanamanku D/1: -7.281677
11-17 10:31:07.516 9032-9032/com.arf.tanamanku D/2: 4.983527183532715
11-17 10:31:07.516 9032-9032/com.arf.tanamanku D/3: 32.00
11-17 10:31:07.516 9032-9032/com.arf.tanamanku D/4: 1008
11-17 10:31:07.516 9032-9032/com.arf.tanamanku D/5: 62  
11-17 10:31:07.516 9032-9032/com.arf.tanamanku D/values: [112.788683, -7.281677, 4.983527183532715, 28.00, 1010, 83]

How do I get all index from fist values and second values? 如何从第一值和第二值获取所有索引? Anyone could help me please? 有人可以帮我吗?

Set the listener to the node one level above (that is to the node which is the root of all your nodes you want to retrieve the values from). 将侦听器设置为上一级节点(即该节点,该节点是要从中检索值的所有节点的根)。 This will give you a HashMap with those nodes keys as the HashMap keys and the nodes values as the HashMap values. 这将为您提供一个HashMap,将那些节点键作为HashMap键,并将节点值作为HashMap值。

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

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