java - 房间抽象 Pojo

标签 java android abstract-class pojo android-room

我正在创建一个跟踪支出的 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/

相关文章:

java - 使用 Android 或 Java 从 MediaWiki/Wikipedia 查询多个结果

java - 一种布局将另一种布局推离屏幕

c# - 从 C# 中反射(reflect)的 OOP 角度来看,动态多态性、抽象类和接口(interface)之间有什么区别?

php - 抽象静态属性不能被覆盖?

java - 为什么IntelliJ在Java自动填充中不显示foreach循环?

java - Eclipse : Used testNG 7 without built with maven ? 类路径问题

android - 具有DiffUtil和LiveData的RecyclerView

Android Html.fromHtml() 在嵌套文本样式上无法正常工作

iphone - iPhone、Android 和黑莓的移动开发

c++ - 为什么我们不能声明一个 std::vector<Abstract Class>?