orderByChild查询不适用于addListenerForSingleValueEvent Firebase

时间:2019-10-03 00:38:00

标签: java android firebase firebase-realtime-database

我正在尝试按子值“ Month”订购数据库,但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();
                            }
                        }

                        @Override
                        public void onCancelled(@NonNull DatabaseError databaseError) {

                        }
                    });
                }

Toast应该按8、9、10顺序打印月份值,但它要按8、10、9顺序打印值

2 个答案:

答案 0 :(得分:0)

我放弃了使用Firebase查询对数据进行排序的方法,我确定我的代码没有任何问题,无论如何,我决定在使用Collections.sort将数据添加到arrayList之后“对其进行排序”,并且它可以正常工作。谢谢

P.S如果有人知道为什么我的Firebase查询无法正常工作,我仍然想知道原因:S

这是代码,以防其他人需要对arrayList中的数据进行排序

//sort the item  to show months in ascending order
Collections.sort(items, new Comparator<PastMonthsWeightStats>(){
    @Override
    public int compare(PastMonthsWeightStats obj1, PastMonthsWeightStats obj2) {
        // ## Ascending order
        // return obj1.firstName.compareToIgnoreCase(obj2.firstName); // To compare string values
        //return Float.valueOf(obj1.getMonthNumber()).compareTo(Float.valueOf(obj2.getMonthNumber())); // To compare integer values

        // ## Descending order
        // return obj2.firstName.compareToIgnoreCase(obj1.firstName); // To compare string values
        return Integer.valueOf(obj2.getMonthNumber()).compareTo(Integer.valueOf(obj1.getMonthNumber())); // To compare integer values
    }
});     

答案 1 :(得分:0)

如果invoicesDB.update({_id: req.params.id}, {'$set': { 'items.$.itemID': 'Here is where you update', }}, function(err) { ... }) 指向此JSON(对于特定用户):

userStatsDatabase

然后您可以使用以下命令按 { "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" } } } 的顺序获取结果:

Month
相关问题