0

I have an activity where I need to select two dates, a first date and an end date. I have two TextView Buttons, which when clicked would display the date pickers. After entering the date, I need to calculate totalCalorie from firstDate to endDate.

But what I got data is calculated totalCalorie from all Dates.

So How can I do this?

Any help would be great, thanks.

This is my codes:

        //************************* FIRST DAY ***************************//
        firstDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Calendar cal = Calendar.getInstance();
                int year = cal.get(Calendar.YEAR);
                int month = cal.get(Calendar.MONTH);
                int day = cal.get(Calendar.DAY_OF_MONTH);

                DatePickerDialog dialog = new DatePickerDialog(
                        UserHighlights.this,
                        android.R.style.Theme_Holo_Light_Dialog_MinWidth, mDateSetListener, year, month, day);
                dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                dialog.show();



            }
        });

        mDateSetListener = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int year, int month, int day) {

                month = month + 1;
                Log.d(TAG, "onDateSet: dd/mm/yyy: " + day + "/" + month + "/" + year);

                String date1= day + "/" + month + "/" + year;
                fDate.setText(date1);
                dateDisplay_first.setText(date1);

                from_Date = year+""+month+""+day;
                onStart(from_Date);

                updateView();
            }
        };

        //************************* END DATE ***************************//
        endDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar cal = Calendar.getInstance();
                int year = cal.get(Calendar.YEAR);
                int month = cal.get(Calendar.MONTH);
                int day = cal.get(Calendar.DAY_OF_MONTH);

                DatePickerDialog dialog = new DatePickerDialog(
                        UserHighlights.this,
                        android.R.style.Theme_Holo_Light_Dialog_MinWidth, mDateSetListener_1, year, month, day);
                dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                dialog.show();

            }
        });

        mDateSetListener_1 = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                month = month + 1;
                Log.d(TAG, "onDateSet: dd/mm/yyy: " + day + "/" + month + "/" + year);

                String date2= day + "/" + month + "/" + year;
                eDate.setText(date2);
                dateDisplay_end.setText(date2);

                end_Date = year+""+month+""+day;
                onStart_1(end_Date);

                updateView();

            }
        };
    }



    private void updateView() {

       final String newFday = fDate.getText().toString().trim();
       final String newEday = eDate.getText().toString().trim();

        ref.orderByChild("dateRecord").startAt(newFday).endAt(newEday);
        ref.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                int count = 0;
                for (DataSnapshot foodTypeSnapshot: dataSnapshot.getChildren()) {

                    String user = String.valueOf((foodTypeSnapshot.child("totalCalorie").getValue()));
                    int totalCount = Integer.parseInt(user);
                    count = count + totalCount;
                    totalCalorie.setText((count  +" kcal"));

                }
                Log.d("TAG", count + "");
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
                throw databaseError.toException();
            }
        });
    }

My Firebase:

my firebase (example)

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441

2 Answers2

1

Your dates are in a format that makes it impossible to select a range. In order to select a range, ensure that the lexicographical order matches the chronological order. In other words: ensure that its a format where year comes before month comes before day.

So any of these are fine:

"2019-11-17"
"2019/11/17"
"20191117"

With values in this format, you can query for child nodes within a date range. For example, to get all nodes in 2019 when the value is in the last format you'd use:

Query query = ref.orderByChild("dateRecord").startAt("20190101").endAt("20191231");
query.addListenerForSingleValueEvent(new ValueEventListener() {
  ...

Also see:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0

You are trying to select range data using String comparison, which doesn't give you the correct data. It's not possible to determine that 18/11/2019 is greater than 19/11/2018 by String comparison. Try to store dateRecord either in TimeInMilis or yyyyMMdd format. Thanks

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46