简体   繁体   中英

How to get all the dates from Firebase and mark the dates in Calendar

I have multiple dates store with each key, under the user ID in "Users Mood" parent.

"Users Mood" : {
 "K2ngvpioRUYF4bRM07Da5cbAjE53" : {     //This is user ID
   "-M3jNjCuGdMCwt1Czpwz" : {           //This is key
     "Date" : "2020-3-30",
     "Scale" : "3"
   },

   "-M3jQWxm7z0EQYgkVenX" : {           //This is key
     "Date" : "2020-3-29",
     "Scale" : "4"
   },

   "-M5hxn-rCJICUvRcMZJu" : {          //This is key
     "Date" : "2020-4-24",
     "Scale" : "2"
   }
 }
}

All the dates (2020-3-30, 2020-3-29, 2020-4-24) should be marked in the calendar. I used a third party calendar which is from https://github.com/Applandeo/Material-Calendar-View

Below are the methods I came up with, but the stored dates did not marked in the calendar.

    userMoodRef=FirebaseDatabase.getInstance().getReference().child("Users Mood").child(user.getUid());
    CalendarView calendarView = (CalendarView) findViewById(R.id.calendar);

    List<EventDay> events = new ArrayList<>();

    ValueEventListener eventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Calendar calendar = Calendar.getInstance();
            ArrayList<String> dates= new ArrayList<>();

            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                String date=ds.child("Date").getValue(String.class);
                dates.add(date);
            }

            for(String date : dates){
                String[] items1 = date.split("-");
                int year= Integer.parseInt(items1[0]);
                int month=Integer.parseInt(items1[1]);
                int day=Integer.parseInt(items1[2]);

                calendar.set(year,month,day);
                events.add(new EventDay(calendar, R.drawable.ic_dot_blue_24dp));
                calendarView.setEvents(events);

            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    };
    userMoodRef.addListenerForSingleValueEvent(eventListener);

Kindly guide me to solve this problem. Thank you.

for(String date : dates){
    Calendar calendar = Calendar.getInstance(); // calendar must be here
    String[] items1 = date.split("-");
    int year= Integer.parseInt(items1[0]);
    int month=Integer.parseInt(items1[1]);
    int day=Integer.parseInt(items1[2]);

    calendar.set(year,month,day);
    events.add(new EventDay(calendar, R.drawable.ic_dot_blue_24dp));
 }

 calendarView.setEvents(events);

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM