Reputation: 1927
I am facing a very small problem while pushing my custom object to firebase. I am trying to explain the problem so the post can be long. I will try to clean the code so as to keep only that which is relevant to the problem. Please be patient while reading my question:)
I am trying to create a small schedule and then when the user presses the Save Button
, I push this schedule to the firebase. I created the below custom objects called Schedule
which has Alarm
which in turn has AlarmTimings
and AlarmDow
objects respectively. I think it will be more clear through the below code.
Schedule.class
@IgnoreExtraProperties
public class Schedule {
private String scheduleName;
private String description;
private Alarm alarm;
public Schedule() {}
public Schedule(String scheduleName, String description, Alarm alarm) {
this.scheduleName = scheduleName;
this.description = description;
this.alarm = alarm;
}
// Setters and Getters
@Exclude
public Map<String, Object> toMapSchedule() {
HashMap<String, Object> result = new HashMap<>();
result.put("scheduleName", scheduleName);
result.put("description", description);
result.put("alarm", alarm);
return result;
}
}
The above class has Alarm Object.
@IgnoreExtraProperties
public class Alarm {
private int date;
private int month;
private int year;
private AlarmTiming alarmTiming;
private AlarmDow alarmDow;
public Alarm() {}
public Alarm(int date, int month, int year, AlarmTiming alarmTiming, AlarmDow alarmDow) {
this.date = date;
this.month = month;
this.year = year;
this.alarmTiming = alarmTiming;
this.alarmDow = alarmDow;
}
//Setters and Getters
@Exclude
public Map<String, Object> toMapAlarm() {
HashMap<String, Object> result = new HashMap<>();
result.put("date", date);
result.put("month", month);
result.put("year", year);
result.put("alarmTiming", alarmTiming);
result.put("alarmDow", alarmDow);
return result;
}
}
The Alarm class in turn has to objects AlarmTimings and AlarmDow.
@IgnoreExtraProperties
public class AlarmDow {
private int sunday, monday, tuesday, wednesday, thursday, friday, saturday, alldays;
public AlarmDow() {}
public AlarmDow(int sunday, int monday, int tuesday, int wednesday, int thursday, int friday, int saturday, int alldays) {
this.sunday = sunday;
this.monday = monday;
this.tuesday = tuesday;
this.wednesday = wednesday;
this.thursday = thursday;
this.friday = friday;
this.saturday = saturday;
this.alldays = alldays;
}
//Setters and Getters
@Exclude
public Map<String, Object> toMapAlarmDow() {
HashMap<String, Object> result = new HashMap<>();
result.put("sunday", sunday);
result.put("monday", monday);
result.put("tuesday", tuesday);
result.put("wednesday", wednesday);
result.put("thursday", thursday);
result.put("friday", friday);
result.put("saturday", saturday);
result.put("alldays", alldays);
return result;
}
}
@IgnoreExtraProperties
public class AlarmTiming {
private int hour1;
private int minute1;
private int hour2;
private int minute2;
private int hour3;
private int minute3;
private int intervalHour;
public AlarmTiming() {}
public AlarmTiming(int hour1, int minute1, int hour2, int minute2, int hour3, int minute3, int intervalHour) {
this.hour1 = hour1;
this.minute1 = minute1;
this.hour2 = hour2;
this.minute2 = minute2;
this.hour3 = hour3;
this.minute3 = minute3;
this.intervalHour = intervalHour;
}
//Normal Setters and Getters
@Exclude
public Map<String, Object> toMapAlarmTiming() {
HashMap<String, Object> result = new HashMap<>();
result.put("hour1", hour1);
result.put("minute1", minute1);
result.put("hour2", hour2);
result.put("minute2", minute2);
result.put("hour3", hour3);
result.put("minute3", minute3);
result.put("intervalHour", intervalHour);
return result;
}
}
The Problem:
The reason I kept the constructors was because in the onCreate()
of my fragment I can initialize them to 0 to something as a default value and then when the user selects something while filling up the schedule the objects get properties get set. Something like below:
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
alarmDow=new AlarmDow(0,0,0,0,0,0,0,0);
alarmTiming= new AlarmTiming(0,0,0,0,0,0,0);
alarm= new Alarm(0,0,0,alarmTiming,alarmDow);
schedule= new Schedule("Null","Null",alarm);
}
Now after the user presses save button. I am doing something like this:
alarm.setAlarmTiming(alarmTiming);
alarm.setAlarmDow(alarmDow);
schedule.setAlarm(alarm);
mRef.child("DailySchedules").child(taskNameEditText.getText().toString())
.setValue(schedule);
Now it doesn't pop-up any errors when i press save. But it doesn't work as well. Nothing gets posted to the Firebase nor does my app crash. Any suggestions :)
Upvotes: 3
Views: 2265
Reputation: 317372
In order to serialize Java objects to Firebase, they should either:
Behave like JavaBeans. This means they have properly named, public getters and setters for each field you want to serialize.
Or use public fields for all data to be serialized.
Right now, your objects only contain private fields and there are not getters and setters. This means Firebase cannot properly introspect your classes to figure out what fields you want to serialize.
Upvotes: 2