房间 ORM 枚举类型转换器错误
Room ORM enum type converter error
我不断收到以下错误:
Cannot figure out how to save this field into database. You can
consider adding a type converter for it.
我已将 @TypeConverter 添加到我的数据库中的一对转换器方法和 @TypeConverters 注释中 class。
DownloadItem.java
@Entity(tableName = "downloads")
public class DownloadItem implements Serializable {
public enum DownloadStatus {
None(0),
Downloading(1),
Suspended(2),
Completed(3),
Deleted(4),
Expired(5);
private final int numeral;
@TypeConverter
public static DownloadStatus getDownloadStatus(int numeral){
for(DownloadStatus ds : values()){
if(ds.numeral == numeral){
return ds;
}
}
return null;
}
@TypeConverter
public static Integer getDownloadStatusInt(DownloadStatus status){
return status.numeral;
}
DownloadStatus(int num){
this.numeral = num;
}
}
@TypeConverters(DownloadStatus.class)
@ColumnInfo(name = "download_status")
public DownloadStatus downloadStatus;
@PrimaryKey
@NonNull
private int contentId;
@Ignore
public DownloadItem() {
}
public DownloadItem(DownloadStatus downloadStatus, int contentId) {
this.downloadStatus = downloadStatus;
this.contentId = contentId;
}
}
DownloadDatabase.java
@Database(entities = {DownloadItem.class}, version = 1)
@TypeConverters({DownloadItem.DownloadStatus.class, PlayerType.class})
public abstract class DownloadDatabase extends RoomDatabase {
public abstract DownloadItemStore downloadItemStore();
private static final String DB_NAME = "download.db";
.......
}
对于getDownloadStatusInt
,应该是int
,而不是Integer
我不断收到以下错误:
Cannot figure out how to save this field into database. You can consider adding a type converter for it.
我已将 @TypeConverter 添加到我的数据库中的一对转换器方法和 @TypeConverters 注释中 class。
DownloadItem.java
@Entity(tableName = "downloads")
public class DownloadItem implements Serializable {
public enum DownloadStatus {
None(0),
Downloading(1),
Suspended(2),
Completed(3),
Deleted(4),
Expired(5);
private final int numeral;
@TypeConverter
public static DownloadStatus getDownloadStatus(int numeral){
for(DownloadStatus ds : values()){
if(ds.numeral == numeral){
return ds;
}
}
return null;
}
@TypeConverter
public static Integer getDownloadStatusInt(DownloadStatus status){
return status.numeral;
}
DownloadStatus(int num){
this.numeral = num;
}
}
@TypeConverters(DownloadStatus.class)
@ColumnInfo(name = "download_status")
public DownloadStatus downloadStatus;
@PrimaryKey
@NonNull
private int contentId;
@Ignore
public DownloadItem() {
}
public DownloadItem(DownloadStatus downloadStatus, int contentId) {
this.downloadStatus = downloadStatus;
this.contentId = contentId;
}
}
DownloadDatabase.java
@Database(entities = {DownloadItem.class}, version = 1)
@TypeConverters({DownloadItem.DownloadStatus.class, PlayerType.class})
public abstract class DownloadDatabase extends RoomDatabase {
public abstract DownloadItemStore downloadItemStore();
private static final String DB_NAME = "download.db";
.......
}
对于getDownloadStatusInt
,应该是int
,而不是Integer