现在,我有一个排行榜,我的应用使用我在 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个也出现在排行榜上。
1 回答
一只萌萌小番薯
TA贡献1795条经验 获得超7个赞
如果我理解正确,您希望加载所有用户,而不必单独加载每个用户。您可以通过在树中将侦听器附加到更高的级别,然后循环遍历子节点来执行此操作:
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
}
});
添加回答
举报
0/150
提交
取消