如何使用 Firebase 将新的真实玩家添加到排行榜?

现在,我有一个排行榜,我的应用使用我在 Firebase 中生成的假玩家显示该排行榜,如下所示:

http://img2.mukewang.com/632bc7650001b07e03020330.jpg

我想知道如何将每个玩我的应用的新玩家的名字添加到排行榜。


现在,我的代码手动接收每个假玩家,我没有一种自动的方式来吸收n个玩家。


user2.addValueEventListener(new ValueEventListener() {

            @Override

            public void onDataChange(DataSnapshot dataSnapshot) {

                // This method is called once with the initial value and again

                // whenever data at this location is updated.

                String value_temp = dataSnapshot.getValue(String.class);

                if(value_temp != null)

                    workshopParticipants.add(new LeaderPlayers(value_temp));

                Collections.sort(workshopParticipants);

                //Log.d(TAG, "Value is: " + mHigh);

            }




            @Override

            public void onCancelled(DatabaseError error) {

                // Failed to read value

                //Log.w(TAG, "Failed to read value.", error.toException());

            }


        });

        user3 = database.getReference(Integer.toString(3));

        user3.addValueEventListener(new ValueEventListener() {

            @Override

            public void onDataChange(DataSnapshot dataSnapshot) {

                // This method is called once with the initial value and again

                // whenever data at this location is updated.

                String value_temp = dataSnapshot.getValue(String.class);

                Log.d(TAG, value_temp);

                if(value_temp != null)

                    workshopParticipants.add(new LeaderPlayers(value_temp));

                Collections.sort(workshopParticipants);

                //Log.d(TAG, "Value is: " + mHigh);

                for(int a = 0; a < workshopParticipants.size(); a++)

                {

                    players.add(workshopParticipants.get(a).getFullName());

                }

                for(int a = 0; a < players.size(); a++){

                    Log.d(TAG, "Players are: " + players.get(a));

                }

            }

这就是我手动添加假玩家的方式。


它目前适用于假玩家,但是如果我有例如,N个真正的玩家玩我的游戏,那么我希望他们所有N个也出现在排行榜上。


慕运维8079593
浏览 91回答 1
1回答

一只萌萌小番薯

如果我理解正确,您希望加载所有用户,而不必单独加载每个用户。您可以通过在树中将侦听器附加到更高的级别,然后循环遍历子节点来执行此操作:database.getReference().addValueEventListener(new ValueEventListener() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onDataChange(DataSnapshot dataSnapshot) {&nbsp; &nbsp; &nbsp; &nbsp; for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d(TAG, userSnapshot.getKey()); // "1", "2"...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d(TAG, userSnapshot.getValue(String.class)); "Christine 20", "Tom 64"...&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onCancelled(DatabaseError error) {&nbsp; &nbsp; &nbsp; &nbsp; Log.w(TAG, "Failed to read value.", error.toException()); // Don't ignore errors&nbsp; &nbsp; }});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java