【发布时间】:2018-06-24 10:05:06
【问题描述】:
我正在创建一个跟踪支出的 Android 应用程序。我正在使用 Room 来保存用户的数据,并且我有显示每日/每周/每月摘要的 POJO。
这些类非常相似,因此我想要一个抽象 POJO,其中包含重新格式化为正确格式的字段和扩展。比如:
public abstract class PeriodInformation {
PeriodInformation(@NonNull Calendar mCalendar, Integer mPeriodSpendingCount, Float mPeriodSpendingSum) {
this.mCalendar = mCalendar;
this.mPeriodSpendingCount = mPeriodSpendingCount;
this.mPeriodSpendingSum = mPeriodSpendingSum;
}
@ColumnInfo(name = "DateTime")
private final Calendar mCalendar;
@ColumnInfo(name = "SpendingCount")
private Integer mPeriodSpendingCount;
@ColumnInfo(name = "SpendingSum")
private Float mPeriodSpendingSum;
// Some other code, e.g., getters, equal override,...
}
这里是扩展名:
public class WeekInformation extends PeriodInformation{
public WeekInformation(@NonNull Calendar mCalendar, Integer mPeriodSpendingCount, Float mMonthSpendingSum) {
super(mCalendar, mPeriodSpendingCount, mMonthSpendingSum);
}
@Override
public String getPeriodRepresentation() {
//return representation;
}
}
但是,我收到以下有关 WeekInformation 类的错误消息:
错误:实体和 Pojos 必须有一个可用的公共构造函数。你可以有一个空的构造函数或一个参数与字段匹配的构造函数(按名称和类型)。
所以这似乎在 Room 中是不可能的,因此我很乐意得到一些建议,如何不必经常复制相同的代码。
谢谢。
编辑: 我使用以下 DAO 代码聚合到 POJO,列 calendarDate 具有以下格式“yyyy-MM-dd'T'HH:mm:ss.SSSXXX”:
@Query("SELECT date(datetime(calendarDate)) AS 'DateTime', count(uID) AS 'SpendingCount', sum(value) AS 'SpendingSum' from spending GROUP BY date(datetime(calendarDate))")
LiveData<List<DayInformation>> loadDayInformation();
【问题讨论】:
标签: java android abstract-class pojo android-room