【问题标题】:Room ORM enum type converter error房间 ORM 枚举类型转换器错误
【发布时间】:2017-11-22 13:15:17
【问题描述】:

我不断收到以下错误:

无法弄清楚如何将此字段保存到数据库中。你可以 考虑为它添加一个类型转换器。

我已将@TypeConverter 添加到我的数据库类上的一对转换器方法和@TypeConverters 注释中。

下载Item.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;
        }
      }

下载Database.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";
.......
}

【问题讨论】:

  • 当类名是DownloadItem时,LTDownloadItem是什么?
  • 很好,我在发帖前更改了班级名称。已更正。 (我在代码中的实际类名都匹配)
  • 我认为 getDownloadStatusInt @TypeConverter 应该是 int,而不是 Integer
  • @AlexCohn 你是对的!!!谢谢!!

标签: android enums orm android-room


【解决方案1】:

getDownloadStatusInt 应该是int,而不是Integer

【讨论】:

  • 是的,就是这样。或者我可以将 getDownloadStatus(int numeric) 更改为 (Integer numeric) 并且也可以。是的,一致性:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-02-21
  • 2013-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多