在Firebase,Android

时间:2017-02-01 15:23:16

标签: android firebase firebase-realtime-database

我希望按分类显示每个用户的分数。

我在Firebase控制台中的数据库结构如下所示

enter image description here

我创建了两个表格得分,其中一个是用户

在分数表中,我使用了用户的ID,GameA的分数,游戏B的分数和游戏C的分数

现在我想以排序方式显示GameA的得分,每个用户都可以在列表视图中看到排序数据,这样他就可以知道他有多少落后于其他人...

在Firebase中,我尝试使用查询,但无法在排序方面取得成功,

   Query queryRef = mDatabase.orderByChild("scores").limitToFirst(100);
    queryRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
                Score score=postSnapshot.getValue(Score.class);
                Log.d("test"," values is " + score.getScoreOfGameA()  + " " + score.getUserId());
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

以上查询结果将出现,但值为0,null

就像下面的

02-02 06:29:09.559 10791-10818/com.firebasedatabaselearn W/PersistentConnection: pc_0 - Using an unspecified index. Consider adding '".indexOn": "scores"' at / to your security and Firebase Database rules for better performance
02-02 06:29:09.562 10791-10791/com.firebasedatabaselearn W/ClassMapper: No setter/field for oFAgix9TlUNS92R2VJaJbNYtH3z2 found on class com.firebasedatabaselearn.module.Score
02-02 06:29:09.562 10791-10791/com.firebasedatabaselearn W/ClassMapper: No setter/field for FrJpw8SMzZYfbECuvhHvbxnbCmh2 found on class com.firebasedatabaselearn.module.Score
02-02 06:29:09.562 10791-10791/com.firebasedatabaselearn D/test:  values is 0 null
02-02 06:29:09.562 10791-10791/com.firebasedatabaselearn W/ClassMapper: No setter/field for NnmawN5gMFVhwaGmZxXgUCPsDMb2 found on class com.firebasedatabaselearn.module.Score
02-02 06:29:09.562 10791-10791/com.firebasedatabaselearn W/ClassMapper: No setter/field for oFAgix9TlUNS92R2VJaJbNYtH3z2 found on class com.firebasedatabaselearn.module.Score
02-02 06:29:09.562 10791-10791/com.firebasedatabaselearn W/ClassMapper: No setter/field for RLEmo9Vyq8Rktwn6Cuz4d6vadml2 found on class com.firebasedatabaselearn.module.Score
02-02 06:29:09.562 10791-10791/com.firebasedatabaselearn W/ClassMapper: No setter/field for FrJpw8SMzZYfbECuvhHvbxnbCmh2 found on class com.firebasedatabaselearn.module.Score
02-02 06:29:09.562 10791-10791/com.firebasedatabaselearn D/test:  values is 0 null

任何人都可以建议我如何实现这个目标?

1 个答案:

答案 0 :(得分:1)

最后,在访问此Firebase orderByChild and equalTo() queries don't work后,我通过以下代码实现了

queryRef = mDatabase.child("scores").orderByChild("scoreOfGameA").limitToLast(100);
        queryRef.addValueEventListener(new ValueEventListener() {

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
                    Score score=postSnapshot.getValue(Score.class);
                    Log.d("test"," values is " + score.getScoreOfGameA());
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });