簡體   English   中英

如何從 Android 中的 Firebase 中獲取基於不同子項的總數據

[英]How to get total of data based on different child from Firebase in Android

每個日期有多個日期和數據(課程和規模)。 每個日期和數據都存儲在一個唯一的鍵下。 日期由日期選擇器插入。

"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)。 請幫助我糾正我的邏輯。

您使用日期的方式不是很有效。 因為這意味着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));
   }

如果您有任何疑問,請隨時發表評論。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM