Astrid Gloria
Astrid Gloria

Reputation: 11

Firebase - Getting a child value under unique key

I am using Google Firebase to store database of user heartbeat from Android Wear. Now I'm trying to retrieve the heartbeat value on child "tmpHR" but the value keeps on showing null.

Here is the structure of my database :

I want to retrieve int tmpHR child value under the random key

Here's my lines of code :

    Integer hrValue;
     ArrayList<Integer> array1;
    array1 = new ArrayList<>();

DatabaseReference userdatabase = FirebaseDatabase.getInstance().getReference().child("Users");
 DatabaseReference ref = userdatabase.child(user.getUid()).child("hrvalue").child("nilaihr");`

`ref.addChildEventListener(new ChildEventListener() {`

            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                for (DataSnapshot uniqueKeySnapshot : dataSnapshot.getChildren()) {
                    //Loop 1 to go through all the child nodes of nilaihr
                    //store random key in uniqueKeySnapshot
                        Integer hrValue = uniqueKeySnapshot.child("tmpHR").getValue(Integer.class);
                        // random key -> child tmpHR -> store in Integer hrValue
                        Log.i("heart rate value", "hrvalue " + hrValue);
                       showData(dataSnapshot);
                       }

            }


            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    }

    private void showData(DataSnapshot dataSnapshot) {array1.add(hrValue);}

And here's the logcat :

05-15 19:01:55.824 27624-27624/com.example.amiramaulina.retrievedataheartbeat I/database array: array database [null, null, null,....., null]
05-15 19:01:55.824 27624-27624/com.example.amiramaulina.retrievedataheartbeat I/heart rate value: hrvalue null

Before, there was only one child (which is Integer "tmpHR") under the random key, and I have succeeded to retrieve the value. Since I added another child (String Date), the value of tmpHR keeps on returning null.

Any solutions would be appreciated. Thank you!

Upvotes: 0

Views: 911

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80952

Try the following:

 ref.addChildEventListener(new ChildEventListener() {

        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {   
                    Integer hrValue = dataSnapshot.child("tmpHR").getValue(Integer.class);
                   showData(dataSnapshot);
           }



        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

Remove the for loop, since you do not need to loop to be able to retrieve tmpHR in this case.

Upvotes: 1

Related Questions