简体   繁体   English

如何从firebase数据库中获取所有子节点?

[英]how to get all child nodes from firebase database?

this is my structure.这是我的结构。 i am trying to code a scoreboard for my app but i cant get the username & score.我正在尝试为我的应用编写记分牌,但我无法获得用户名和分数。 i want to safe that username & score in a sorted list to get the top 10.我想在排序列表中保护该用户名和分数以获得前 10 名。

this is my code: i onle get the usernames.这是我的代码:我只得到用户名。

databaseReference = FirebaseDatabase.getInstance().getReference("Users");
    databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Userlist = new ArrayList<String>();
                // Result will be holded Here

                for (DataSnapshot dsp : dataSnapshot.getChildren()) {
                    for (DataSnapshot as : dataSnapshot.getChildren()) {
                        String userkey= as.getKey();
                        Userlist.add(userkey); //add result into array list

                    }
                }

                un.setText(Userlist.get(2));

            }

        @Override
        public void onCancelled(DatabaseError error) {

        }
    });

在此处输入图片说明

Try the following:请尝试以下操作:

databaseReference = FirebaseDatabase.getInstance().getReference("Users");
databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Userlist = new ArrayList<String>();
            // Result will be holded Here

            for (DataSnapshot dsp : dataSnapshot.getChildren()) {
                    String userkey  = dsp.getKey();
                    String scores   = dsp.child("scorehigh").getValue().toString();
                    Userlist.add(userkey); //add result into array list
                }
            }

            un.setText(Userlist.get(2));

        }

    @Override
    public void onCancelled(DatabaseError error) {

    }
});

The reference is at Users then you need to loop twice to be able to access the attributes scorehigh and username参考是在Users然后你需要循环两次才能访问属性scorehighusername

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

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