无法弄清楚如何将此字段保存到数据库中

时间:2018-05-27 22:13:45

标签: android database android-room

我为我在Android应用程序中创建的对象创建了一个类型转换器。

我不知道为什么,但是编译器告诉我它无法弄清楚如何将这个字段保存到数据库中;即使我为它创建了一个TypeConverter。

我做错了什么?

  

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

     

屏幕截图[]截图;

Game.class

@Entity(indices = {@Index(value = {"id"}, unique = true)})
    public class Game {

        @PrimaryKey
        @NonNull
        private int id;
        private String name, url;

        @TypeConverters(IntArrayTypeConverter.class)
        private int[] platforms, genres, developers;
        @TypeConverters(CoverTypeConverter.class)
        private Cover cover;
        @TypeConverters(EsrbTypeConverter.class)
        private Esrb esrb;
        @TypeConverters(ScreenshotTypeConverter.class)
        private Screenshot[] screenshots;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public int getCreated_at() {
            return created_at;
        }

        public void setCreated_at(int created_at) {
            this.created_at = created_at;
        }

        public int getUpdated_at() {
            return updated_at;
        }

        public void setUpdated_at(int updated_at) {
            this.updated_at = updated_at;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public int[] getPlatforms() {
            return platforms;
        }

        public void setPlatforms(int[] platforms) {
            this.platforms = platforms;
        }

        public int[] getGenres() {
            return genres;
        }

        public void setGenres(int[] genres) {
            this.genres = genres;
        }

        public Cover getCover() {
            return cover;
        }

        public void setCover(Cover cover) {
            this.cover = cover;
        }

        public Esrb getEsrb() {
            return esrb;
        }

        public void setEsrb(Esrb esrb) {
            this.esrb = esrb;
        }

        public Screenshot[] getScreenshots() {
            return screenshots;
        }

        public void setScreenshots(Screenshot[] screenshots) {
            this.screenshots = screenshots;
        }

        public int[] getDevelopers() {
            return developers;
        }

        public void setDevelopers(int[] developers) {
            this.developers = developers;
        }
    }

ScreenshotTypeConverter.class

public class ScreenshotTypeConverter {

    @TypeConverter
    public static Screenshot[] fromString(String screenshotsJson) {
        if (screenshotsJson == null) return null;

        Gson gson = new Gson();
        Screenshot[] screenshots = gson.fromJson(screenshotsJson, Screenshot[].class);
        return screenshots;
    }

    @TypeConverter
    public static String toString(Screenshot screenshot) {
        if (screenshot == null) return null;

        Gson gson = new Gson();
        String screenshotsJson = gson.toJson(screenshot);
        return screenshotsJson;
    }
}

解决:

我刚刚意识到我没有接受我的ScreenshotTypeConverter方法中的正确参数。

    @TypeConverter
    public static String toString(Screenshot[] screenshots) {
        if (screenshot == null) return null;

        Gson gson = new Gson();
        String screenshotsJson = gson.toJson(screenshots);
        return screenshotsJson;
    }
}

0 个答案:

没有答案