简体   繁体   English

Firebase-在唯一键下获取子值

[英]Firebase - Getting a child value under unique key

I am using Google Firebase to store database of user heartbeat from Android Wear. 我正在使用Google Firebase存储来自Android Wear的用户心跳数据库。 Now I'm trying to retrieve the heartbeat value on child "tmpHR" but the value keeps on showing null. 现在,我正在尝试检索子级“ tmpHR”的心跳值,但该值一直显示为空。

Here is the structure of my database : 这是我的数据库的结构:

I want to retrieve int tmpHR child value under the random key 我想在随机键下检索int tmpHR子值

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 : 这是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. 以前,随机密钥下只有一个孩子(即整数“ tmpHR”),并且我已经成功检索了该值。 Since I added another child (String Date), the value of tmpHR keeps on returning null. 由于我添加了另一个孩子(字符串日期),因此tmpHR的值始终返回null。

Any solutions would be appreciated. 任何解决方案将不胜感激。 Thank you! 谢谢!

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. 删除for循环,因为在这种情况下不需要循环就可以检索tmpHR

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

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