-1

I have created a custom calendar which uses an array "HomeCollection" taken from github link https://github.com/DeveloperBrothers/Custom-Calendar-View in order to create a custom calendar in my android application. Currently the Events data is hard-coded into this array (see code snippet below) but wish to take the event attributes from my firebase database instead where I have created a database and saved such Event data (see picture link below).

HomeCollection.date_collection_arr = new ArrayList<HomeCollection>();
        HomeCollection.date_collection_arr.add(new HomeCollection("2018-11-21", "Winter Table Quiz", "7-10 pm, Clubhouse", "ClubHouse", "Come along to the Winter Table Quiz, €5pp, tables of 4, theres lots of great prizes to be won."));
       HomeCollection.date_collection_arr.add(new HomeCollection("2018-11-03", "u21 Division 1 Training ", "10-11am, Club Grounds", "ClubHouse", "Weekly Division 1 training. Weather dependant."));
        HomeCollection.date_collection_arr.add(new HomeCollection("2018-11-03", "u21 Division 2 Training ", "11-12am, Club Grounds", "ClubHouse", "Weekly Division 2 training. Weather dependant."));
        HomeCollection.date_collection_arr.add(new HomeCollection("2018-11-10", "u21 Division 1 Training ", "10-11am, Club Grounds", "ClubHouse", "Weekly Division 1 training. Weather dependant."));
        HomeCollection.date_collection_arr.add(new HomeCollection("2018-11-10", "u21 Division 2 Training ", "11-12am, Club Grounds", "ClubHouse", "Weekly Division 2 training. Weather dependant."));
        HomeCollection.date_collection_arr.add(new HomeCollection("2018-11-17", "u21 Division 1 Training ", "10-11am, Club Grounds", "ClubHouse", "Weekly Division 1 training. Weather dependant."));
        HomeCollection.date_collection_arr.add(new HomeCollection("2018-11-17", "u21 Division 2 Training ", "11-12am, Club Grounds", "ClubHouse", "Weekly Division 2 training. Weather dependant."));
        HomeCollection.date_collection_arr.add(new HomeCollection("2018-11-24", "u21 Division 1 Training ", "10-11am, Club Grounds", "ClubHouse", "Weekly Division 1 training. Weather dependant."));
        HomeCollection.date_collection_arr.add(new HomeCollection("2018-11-24", "u21 Division 2 Training ", "11-12am, Club Grounds", "ClubHouse", "Weekly Division 2 training. Weather dependant."));*/
        cal_month = (GregorianCalendar) GregorianCalendar.getInstance();
        cal_month_copy = (GregorianCalendar) cal_month.clone();
        hwAdapter = new HwAdapter(this, cal_month, HomeCollection.date_collection_arr);

Could anyone help me in changing this static code to my Firebase database data?

Link to my firebase JSON events tree image

2 Answers2

0

So you can make a Java bean for saving all this data. Make getter and setter methods for all the data that you are storing as an object. For example, make the getter and setter methods for the objects properties like : eventDate,eventDescription,etc and say you name your bean as EventBean.java.

Now use the childEventListener like so :

mDatabaseReference = mFirebaseDatabase.getReference().getChild("event");
mDatabaseReference.addChildEventListener(new OnChildEventListener(){

@Override
public void onChildAdded(DataSnapshot snapshot, String previousChildName){
EventBean data = snapshot.getValue(EventBean.class);//now you have got data in the 
eventbean instance
System.out.println(data.getEventDate());
}
});

I hope this is what you are looking for.

Sagar Balyan
  • 620
  • 6
  • 20
0

1) you have to get the firebase data reference in your code

DatabaseReference mFireBaseDatabaseReference = FirebaseDatabase.getInstance().getReference();

2) then you have add the firebase listener to get the data:

mFireBaseDatabaseReference.child("events").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                //you can parse the data in your models here like this
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
Event event = childSnapshot.getValue(Event.class);
HomeCollection.date_collection_arr.add(event);
}
            }

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

            }
        });
  • you can follow the firebase android documentation to know more about firebase realtimedatabase: https://firebase.google.com/docs/database/android/lists-of-data – mudassar raza Jan 23 '19 at 17:49
  • I recieve an error when using the above code but it gave me the idea of the following code: – NewToAndroid97 Jan 24 '19 at 18:23
  • mFireBaseDatabaseReference.child("events").addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot dataSnapshot) { for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) { Event event = childSnapshot.getValue(Event.class); HomeCollection.date_collection_arr.add(new HomeCollection(event.getEventDate(),event.getEventName(),event.getEventTime(),event.getEventLocation(),event.getEventDescription())); } – NewToAndroid97 Jan 24 '19 at 18:24
  • but event.getTime() etc doesnt seem to pull the data from firebase into the array – NewToAndroid97 Jan 24 '19 at 18:24