我尝试按子值“月”对数据库进行排序,但 orderByChild 不起作用。当我使用 onChildEventListener 而不是 addListenerForSingleValueEvent 时它可以工作但我不想使用 childEventListener
这是我的数据库
"Stats" : {
"2019" : {
"YxdZZHGy3rW4xdjORfk2i5mFRYG2" : {
"August" : {
"Weight" : {
"Body_Fat" : "29",
"Month" : 8,
"Weight" : "68"
}
},
"October" : {
"Weight" : {
"Body_Fat" : "29",
"Month" : 10,
"Weight" : "67"
}
},
"September" : {
"Weight" : {
"Body_Fat" : "28.5",
"Month" : 9,
"Weight" : "65.5"
}
}
}
}
},
这是java代码
userStatsDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
Query query = userStatsDatabase.child(ds.getKey()).child("Weight").orderByChild("Month").limitToFirst(12);
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
Map<String, Object> map = (Map<String, Object>) dataSnapshot.getValue();
pastMonthsBodyFat = Float.parseFloat(map.get("Body_Fat").toString());
pastMonthsWeight = Float.parseFloat(map.get("Weight").toString());
pastWeightList.add(pastMonthsWeight);
pastBodyFatList.add(pastMonthsBodyFat);
Toast.makeText(Stats.this, map.get("Month").toString(), Toast.LENGTH_SHORT).show();
}
Toast 应该按顺序打印月份值 8,9,10,但它打印的是值 8,10,9
12345678_0001
慕娘9325324
相关分类