【问题标题】:How do I add new, real players to a leaderboard using Firebase?如何使用 Firebase 将新的真实玩家添加到排行榜?
【发布时间】:2019-04-05 03:03:42
【问题描述】:

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

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

现在,我的代码手动接收每个假玩家,我没有自动接收 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 个也出现在排行榜上。

【问题讨论】:

    标签: java android firebase-realtime-database


    【解决方案1】:

    如果我对您的理解正确,您希望加载所有用户,而不必单独加载每个用户。您可以通过在树中将侦听器附加到更高一级,然后遍历子节点来做到这一点:

    database.getReference().addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
                Log.d(TAG, userSnapshot.getKey()); // "1", "2"...
                Log.d(TAG, userSnapshot.getValue(String.class)); "Christine 20", "Tom 64"...
            }
        }
    
    
        @Override
        public void onCancelled(DatabaseError error) {
            Log.w(TAG, "Failed to read value.", error.toException()); // Don't ignore errors
        }
    
    });
    

    【讨论】:

      猜你喜欢
      • 2019-03-22
      • 1970-01-01
      • 2020-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-09
      • 2018-07-28
      相关资源
      最近更新 更多