是否可以在新的Android体系结构组件和房间持久性库中将Enum类型用作实体类中的嵌入字段?Android体系结构组件:使用枚举
我的实体(带嵌入式枚举):
@Entity(tableName = "tasks")
public class Task extends SyncEntity {
@PrimaryKey(autoGenerate = true)
String taskId;
String title;
/** Status of the given task.
* Enumerated Values: 0 (Active), 1 (Inactive), 2 (Completed)
*/
@Embedded
Status status;
@TypeConverters(DateConverter.class)
Date startDate;
@TypeConverters(StatusConverter.class)
public enum Status {
ACTIVE(0),
INACTIVE(1),
COMPLETED(2);
private int code;
Status(int code) {
this.code = code;
}
public int getCode() {
return code;
}
}
}
我的TypeConverter:
public class StatusConverter {
@TypeConverter
public static Task.Status toStatus(int status) {
if (status == ACTIVE.getCode()) {
return ACTIVE;
} else if (status == INACTIVE.getCode()) {
return INACTIVE;
} else if (status == COMPLETED.getCode()) {
return COMPLETED;
} else {
throw new IllegalArgumentException("Could not recognize status");
}
}
@TypeConverter
public static Integer toInteger(Task.Status status) {
return status.getCode();
}
}
当我编译此,我得到一个错误说“错误:(52,12)错误:实体和Pojos必须有一个可用的公共构造函数。你可以有一个空的构造函数或者一个构造函数,它们的参数匹配字段(按名称和类型)。'
更新1 我SyncEntity类:
/** * 为同步所有客房实体基类。 */
@Entity
public class SyncEntity {
@ColumnInfo(name = "created_at")
Long createdAt;
@ColumnInfo(name = "updated_at")
Long updatedAt;
}
是否'SyncEntity'定义任何构造函数? – CommonsWare
不,它不。更新了SyncEntity.class的问题。 – Bohsen
我认为你或者需要让你的领域'public',提供'public' setter,或者提供一个'public'构造函数来匹配你的'@ Query'列。否则,Room无法为您提供数据。你唯一的构造函数是零参数。 – CommonsWare