房间抽象波霍

时间:2018-06-24 10:05:07

标签: 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类的以下错误消息:

  

错误:实体和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();

1 个答案:

答案 0 :(得分:0)

我能够使用Embedded批注为我完成这项工作,从而可以直接访问嵌入式数据类型的字段。

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}