我正在创建一个跟踪支出的 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 类的以下错误消息:
error: Entities and Pojos must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type).
所以这似乎在 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();
最佳答案
我能够使用嵌入式批注为我完成这项工作,允许直接访问嵌入式数据类型的字段。
public class DayInformation {
@Embedded
public PeriodInformation periodInformation;
@Override
public String toString() {
return "DayInformation{" +
"periodInformation=" + periodInformation +
'}';
}
}
和
public class PeriodInformation {
PeriodInformation(Calendar timestamp,
int periodSpendingCount,
float periodSpendingSum) {
this.timestamp = timestamp;
this.periodSpendingCount = periodSpendingCount;
this.periodSpendingSum = periodSpendingSum;
}
@ColumnInfo(name = "DateTime")
public final Calendar timestamp;
@ColumnInfo(name = "SpendingCount")
public Integer periodSpendingCount;
@ColumnInfo(name = "SpendingSum")
public Float periodSpendingSum;
@Override
public String toString() {
final DateFormat dateInstance = SimpleDateFormat.getDateInstance();
String date = timestamp == null ? "null" : dateInstance.format(timestamp.getTime());
return "PeriodInformation{" +
"timestamp='" + date + '\'' +
", periodSpendingCount=" + periodSpendingCount +
", periodSpendingSum=" + periodSpendingSum +
'}';
}
}
加上
@Entity
public class Spending {
@PrimaryKey(autoGenerate = true)
public int uid;
@ColumnInfo(name = "calendarDate")
public Calendar timestamp;
@ColumnInfo(name = "value")
public float value;
public Spending(@NonNull Calendar timestamp, float value) {
this.timestamp = timestamp;
this.value = value;
}
@Override
public String toString() {
final DateFormat dateInstance = SimpleDateFormat.getDateInstance();
String date = timestamp == null ? "null" : dateInstance.format(timestamp.getTime());
return "Spending{" +
"uid=" + uid +
", timestamp='" + date + '\'' +
", value=" + value +
'}';
}
}
和一个 DAO
@Dao
public interface SpendingDao {
@Insert
void insertAll(Spending... spendings);
@Query("SELECT * FROM spending")
LiveData<List<Spending>> findAll();
@Query("SELECT calendarDate AS 'DateTime', count(uID) AS 'SpendingCount', sum(value) AS 'SpendingSum' from spending GROUP BY date(datetime(calendarDate))")
LiveData<List<DayInformation>> loadDayInformation();
}
给出以下输出
aggregated data is
DayInformation{periodInformation=PeriodInformation{timestamp='Jun 26, 2018', periodSpendingCount=8, periodSpendingSum=184.0}}
spending data is Spending{uid=1, timestamp='Jun 26, 2018', value=23.0}
spending data is Spending{uid=2, timestamp='Jun 26, 2018', value=23.0}
spending data is Spending{uid=3, timestamp='Jun 26, 2018', value=23.0}
spending data is Spending{uid=4, timestamp='Jun 26, 2018', value=23.0}
spending data is Spending{uid=5, timestamp='Jun 26, 2018', value=23.0}
spending data is Spending{uid=6, timestamp='Jun 26, 2018', value=23.0}
spending data is Spending{uid=7, timestamp='Jun 26, 2018', value=23.0}
spending data is Spending{uid=8, timestamp='Jun 26, 2018', value=23.0}
关于java - 房间抽象 Pojo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51008855/