简体   繁体   中英

How to retrieve all value of child from Firebase?

I have this database in Firebase, this my database structure

My Database.json

My 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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