Firebase Orderby查询无效

时间:2017-01-15 08:18:02

标签: android firebase firebase-realtime-database

我想在w上使用orderby得分Json。

我的Json

{  "score": {
"Abc": {
  "value": 5,
  "w": 60
},
"RRRR": {
  "value": 3,
  "w": 20
},
"Ral": {
  "value": 4,
  "w": 50
}  }}

我的Android代码用于获取分数数据。

  myRef.child("score").orderByChild("w").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            //  String value = dataSnapshot.getValue(String.class);
            Log.d(TAG, "Value is: " + dataSnapshot.toString());
        }

        @Override
        public void onCancelled(DatabaseError error) {
            // Failed to read value
            Log.w(TAG, "Failed to read value.", error.toException());
        }
    });

将o / p设为(错误)

DataSnapshot { key = score, value = {Ral={w=50, value=4}, RRRR={w=20, value=3}, Abc={w=60, value=5}} }

我想在分数上使用w in in order并期望

{  "score": {
"RRRR": {
  "value": 3,
  "w": 20
},
"Ral": {
  "value": 4,
  "w": 50
},
"Abc": {
  "value": 5,
  "w": 60
}  }}

1 个答案:

答案 0 :(得分:2)

尝试迭代以下结果(假设您有与分数数据对应的Score java对象)

for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
    Score score = childSnapshot.getValue(Score.class);
}