Google JetPack Room不支持 泛型的TypeConverter 吗?

使用room后感觉上手还挺好的,就是遇到了个转换器的问题

其实我就想保存外层List数据在数据库,里面List数据也是很重要的,想保存在一个表,里层只能先保存jsonString形式了,然后就遇到转换器的问题。

数据模型:

public class MyBean {
    private List<DataBean> data;

    public List<DataBean> getData() {
        return data;
    }

    public void setData(List<DataBean> data) {
        this.data = data;
    }
    
    @Entity(tableName = "data_table")
    @TypeConverters({ AConverts.class, BConverts.class })
    public static class DataBean implements Serializable {

        @PrimaryKey
        private long id;
        private long account_id;

        @ColumnInfo(name = "a_column")
        private List<A> aList;
        @ColumnInfo(name = "b_column")
        private List<B> bList;

        public long getId() {
            return id;
        }

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

        public long getAccountId() {
            return id;
        }

        public void setAccountId(long account_id) {
            this.account_id = account_id;
        }

        public List<A> getA() {
            return aList;
        }

        public void setA(List<A> aList) {
            this.aList = aList;
        }

        public List<B> getB() {
            return bList;
        }

        public void setB(List<B> bList) {
            this.bList = bList;
        }

        public static class A {

            private int start_time;

            public int getStart_time() {
                return start_time;
            }

            public void setStart_time(int start_time) {
                this.start_time = start_time;
            }
        }

        public static class B {
            private int weekday;

            public int getWeekday() {
                return weekday;
            }

            public void setWeekday(int weekday) {
                this.weekday = weekday;
            }
        }
    }
}

转换器

public class AConverts {
    @TypeConverter
    public List<DataBean.A> stringToObject(String value) throws IXJsonErrorException {
        Gson gson = new Gson();
        if (value == null) {
            return Collections.emptyList();
        }
        Type listType = new TypeToken<List<DataBean.A>>() {}.getType();
        return gson.fromJson(value, listType);
    }

    @TypeConverter
    public String objectToString(List<DataBean.A> aList) {
        Gson gson = new Gson();
        return gson.toJson(aList);
    }
}


public class BConverts {
    @TypeConverter
    public List<DataBean.B> stringToObject(String value) throws IXJsonErrorException {

        Gson gson = new Gson();
        if (value == null) {
            return Collections.emptyList();
        }
        Type listType = new TypeToken<List<DataBean.B>>() {}.getType();
        return gson.fromJson(value, listType);
    }

    @TypeConverter
    public String objectToString(List<DataBean.B> bList) {
        Gson gson = new Gson();
        return gson.toJson(bList);
    }
}

如果使用泛型添加一个通用的转换器则会报错

这个才两个转换器

我的项目中其实有四个,这个岂不是要写死我。。。。。

如果新添加需要转换器的类岂不是又要写。。。。

有没有遇到同样痛苦的同学,欢迎留言探讨

发布了45 篇原创文章 · 获赞 6 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/u010436867/article/details/103237975