我将自定义对象推送到firebase时遇到了一个非常小的问题。我试图解释这个问题所以帖子可能很长。我将尝试清理代码,以便只保留与问题相关的代码。请在阅读我的问题时保持耐心:)
我正在尝试创建一个小计划,然后当用户按下Save Button
时,我将此计划推送到firebase。我创建了以下名为Schedule
的自定义对象,其中Alarm
分别包含AlarmTimings
和AlarmDow
个对象。我认为通过以下代码会更清楚。
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;
}
}
上面的类有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;
}
}
警报类依次对象为AlarmTimings和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;
}
}
问题:
我保留构造函数的原因是因为在我的片段的onCreate()
中我可以将它们初始化为0作为默认值,然后当用户在填充日程表时选择某些内容时,对象获取属性得到设置。如下所示:
@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);
}
现在用户按下保存按钮。我正在做这样的事情:
alarm.setAlarmTiming(alarmTiming);
alarm.setAlarmDow(alarmDow);
schedule.setAlarm(alarm);
mRef.child("DailySchedules").child(taskNameEditText.getText().toString())
.setValue(schedule);
现在按下保存时不会弹出任何错误。但它不起作用。什么都没有发布到Firebase,我的应用程序也没有崩溃。任何建议:)
答案 0 :(得分:2)
为了将Java对象序列化为Firebase,他们应该:
表现得像JavaBeans。这意味着他们为您要序列化的每个字段都有正确命名的公共getter和setter。
或者使用公共字段来序列化所有数据。
现在,您的对象只包含私有字段,并且没有getter和setter。这意味着Firebase无法正确地对您的类进行内省,以确定要序列化的字段。