orderByChild 查询不适用于 addListenerForSingleValueEvent

我尝试按子值“月”对数据库进行排序,但 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


拉莫斯之舞
浏览 84回答 2
2回答

12345678_0001

如果userStatsDatabase指向此 JSON(对于特定用户):&nbsp; {&nbsp; &nbsp; "August" : {&nbsp; &nbsp; &nbsp; "Weight" : {&nbsp; &nbsp; &nbsp; &nbsp; "Body_Fat" : "29",&nbsp; &nbsp; &nbsp; &nbsp; "Month" : 8,&nbsp; &nbsp; &nbsp; &nbsp; "Weight" : "68"&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; },&nbsp; &nbsp; "October" : {&nbsp; &nbsp; &nbsp; "Weight" : {&nbsp; &nbsp; &nbsp; &nbsp; "Body_Fat" : "29",&nbsp; &nbsp; &nbsp; &nbsp; "Month" : 10,&nbsp; &nbsp; &nbsp; &nbsp; "Weight" : "67"&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; },&nbsp; &nbsp; "September" : {&nbsp; &nbsp; &nbsp; "Weight" : {&nbsp; &nbsp; &nbsp; &nbsp; "Body_Fat" : "28.5",&nbsp; &nbsp; &nbsp; &nbsp; "Month" : 9,&nbsp; &nbsp; &nbsp; &nbsp; "Weight" : "65.5"&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; }然后你可以按以下顺序获得结果Month:Query query = userStatsDatabase..orderByChild("Weight/Month");query.addListenerForSingleValueEvent(new ValueEventListener() {&nbsp; @Override&nbsp; public void onDataChange(@NonNull DataSnapshot dataSnapshot) {&nbsp; &nbsp; for (DataSnapshot monthSnapshot: dataSnapshot.getChildren()) {&nbsp; &nbsp; &nbsp; System.out.println(monthSnapshot.getKey()); // August, September, October&nbsp; &nbsp; &nbsp; System.out.println(monthSnapshot.child("Weight/Month").getValue(Long.class)); // 8, 9, 10&nbsp; &nbsp; }&nbsp; }&nbsp; ...

慕娘9325324

我放弃了使用 Firebase 查询对数据进行排序,我确信我的代码没有任何问题,无论如何,我决定使用 Collections.sort 将数据添加到 arrayList“之后”对其进行排序,并且它有效。谢谢PS,如果有人知道为什么我的 firebase 查询不起作用,我仍然想知道原因:S这是代码,以防其他人需要对 arrayList 中的数据进行排序//sort the item&nbsp; to show months in ascending orderCollections.sort(items, new Comparator<PastMonthsWeightStats>(){&nbsp; &nbsp; @Override&nbsp; &nbsp; public int compare(PastMonthsWeightStats obj1, PastMonthsWeightStats obj2) {&nbsp; &nbsp; &nbsp; &nbsp; // ## Ascending order&nbsp; &nbsp; &nbsp; &nbsp; // return obj1.firstName.compareToIgnoreCase(obj2.firstName); // To compare string values&nbsp; &nbsp; &nbsp; &nbsp; //return Float.valueOf(obj1.getMonthNumber()).compareTo(Float.valueOf(obj2.getMonthNumber())); // To compare integer values&nbsp; &nbsp; &nbsp; &nbsp; // ## Descending order&nbsp; &nbsp; &nbsp; &nbsp; // return obj2.firstName.compareToIgnoreCase(obj1.firstName); // To compare string values&nbsp; &nbsp; &nbsp; &nbsp; return Integer.valueOf(obj2.getMonthNumber()).compareTo(Integer.valueOf(obj1.getMonthNumber())); // To compare integer values&nbsp; &nbsp; }});&nbsp; &nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java