【问题标题】:How to get total of data based on different child from Firebase in Android如何从 Android 中的 Firebase 中获取基于不同子项的总数据
【发布时间】:2020-05-03 15:55:36
【问题描述】:

每个日期都有多个日期和数据(课程和规模)。每个日期和数据都存储在一个唯一的键下。日期由日期选择器插入。

"Data":{
   "K2ngvpioRUYF4bRM07Da5cbAjE53":{   //UID
      "-M3jNjCuGdMCwt1Czpwz":{        //unique key
         "Date":"2020-3-30",
         "Course":"A",
         "Scale":"3"
      },
      "-M5hxnQrCJdCUvRcMZJu":{
         "Date":"2020-4-24",
         "Course":"A",
         "Scale":"3"
      }
      "-M3jQWxm7z0EQYgkVenX":{
         "Date":"2020-4-29",
         "Course":"B",
         "Scale":"4"
      },
      "-M5hxn-rCJICUvRcMZJu":{
         "Date":"2020-4-24",
         "Course":"B",
         "Scale":"2"
      }
   }
}

我想根据每月的课程计算总秤数。对于比例,如果数字为 3,则表示 0.6。如果数字为 2,则表示 0.4。如果数字是 4,则表示 0.8。因此,对于课程 A,总数为 1.2(0.6+0.6)。对于课程 B,总数为 1.2 (0.8+0.4)。

   Calendar mCalendar = Calendar.getInstance();
    int year = mCalendar.get(Calendar.YEAR);
    int month = mCalendar.get(Calendar.MONTH);
    final int[] Currmonth = {month + 1};         //this is array because there are other method using this but unnecessary for this question
    int lastDay = mCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    int firstDay=1;
    String start=year+"-"+Currmonth[0]+"-"+firstDay;
    String end=year+"-"+Currmonth[0]+"-"+lastDay;
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Users Mood");
    Query range = ref.child(user.getUid()).orderByChild("Date").startAt(start).endAt(end);
    range.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            ArrayList<String> courses=new ArrayList<>();
            Map<String, Double> hm = new HashMap<>();
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                String course=ds.child("Course").getValue(String.class);
                courses.add(course);
                String scale=ds.child("Scale").getValue(String.class);

                for(String i :courses) {
                    double num=0;
                    if (hm.get(i).equals("1")) {
                         num=0.2;
                    }else if(hm.get(i).equals("2")){
                         num=0.4;
                    }else if(hm.get(i).equals("3")){
                         num=0.6;
                    }else if(hm.get(i).equals("4")){
                         num=0.8;
                    }else if(hm.get(i).equals("5")){
                         num=1;
                    }
                    double total = hm.containsKey(course) ? hm.get(course) : 0;
                    total += num;
                    hm.put(course, total);
                }
           }
            for(String key:hm.keySet()) {
                Log.d("tag", key+hm.get(key));
            }

        }

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

        }
    });

在这种情况下,查询是从当月的开始日期到当月的结束日期。映射两个孩子的逻辑是不正确的。我认为 hashmap 不适合这种情况,因为同一课程的相同比例的数据可能很少(例如,有两个相同的数据,课程 A 比例为 3 或 num 0.6)。请帮我纠正我的逻辑。

【问题讨论】:

    标签: java android firebase-realtime-database


    【解决方案1】:

    您使用约会对象的方式效率不高。因为这意味着2020-4-122020-4-4 之前。我建议

    //year is integer year
    String monthStr = month; //month is integer month
    if(month<=9)monthStr="0"+monthStr;
    String dateStr = date; //date is integer date
    if(date<=9)dateStr="0"+dateStr;
    String finalStr = year+"-"+monthStr+"-"+dateStr;
    

    话虽如此,您在不必要地使用ArrayList。与您所说的相反,这里可以使用 HashMap。 hm.get(i) 也将返回 Double ,我不知道您为什么要将它与字符串进行比较,而实际上您应该比较字符串比例。不管怎样,看看我对你的循环体所做的修改。我希望您知道 parseDouble 将 String 转换为其 Double 等价物。

    for(DataSnapshot ds : dataSnapshot.getChildren()) {
                String course=ds.child("Course").getValue(String.class);
                String scale=ds.child("Scale").getValue(String.class);
    
                    double num= Double.parseDouble(scale)/5.0;
    
                    double total = hm.containsKey(course) ? hm.get(course) : 0;
                    total += num;
                    hm.put(course, total);
    
       }
       for(String key:hm.keySet()) {
            Log.d("tag", key+hm.get(key));
       }
    

    如果您有任何疑问,请随时发表评论。

    【讨论】:

    • 您能详细解释一下“2020-4-12 早于 2020-4-4”的说法吗?
    • @Mouris Firebase 按字典顺序对项目进行排序。这意味着 2020-4-12 在 2020-4-4 之前,因为 1 在 4 之前。但按时间顺序,4 月 4 日在 4 月 12 日之前。因此,如果为个位数整数添加零,它将起作用。然后 2020-04-04 在 2020-04-12 之前,因为 0 在 1 之前,与时间顺序一致。
    猜你喜欢
    • 2021-12-26
    • 1970-01-01
    • 2013-06-17
    • 2021-01-25
    • 1970-01-01
    • 1970-01-01
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多