当前位置:网站首页>Cannot figure out how to save this field into database. You can consider adding a type converter for
Cannot figure out how to save this field into database. You can consider adding a type converter for
2022-08-02 14:06:00 【慢行的骑兵】
- 一.场景:使用Room框架,报错,Cannot figure out how to save this field into database. You can consider adding a type converter for it。在ClassMomentInfo类中包含了两个成员变量,mMediaBeans和mClazzs,运行时候提示以上错误。错误代码如下:
@Entity(tableName = "classMomentInfo")
public class ClassMomentInfo {
@PrimaryKey(autoGenerate=true)
private long uid;
//省略代码...
//ROOM不支持直接存储集合
//@TypeConverters(MediaBeanTypeConverter.class)
private List<MediaBean> mMediaBeans;
//@TypeConverters(StringTypeConverter.class)
private List<String> mClazzs;
//省略代码...
}
- 二.原因,Room不支持直接存储列表的功能
- 三.解决方案
- 3.1.新建类XxxConverter,代码如下:
import android.arch.persistence.room.TypeConverter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.List;
/** * describe:使用Room框架,room不支持对象中直接存储集合 * https://stackoverflow.com/questions/53812636/android-error-cannot-figure-out-how-to-save-this-field-into-database-you-can */
public class StringTypeConverter {
Gson gson = new Gson();
@TypeConverter
public List<String> stringToSomeObjectList(String data) {
if (data == null) {
return Collections.emptyList();
}
Type listType = new TypeToken<List<String>>() {
}.getType();
return gson.fromJson(data, listType);
}
@TypeConverter
public String someObjectListToString(List<String> someObjects) {
return gson.toJson(someObjects);
}
}
- 3.2.接着在ClassMomentInfo类中,在成员变量mClazzs上方添加"@TypeConverters(StringTypeConverter.class)";
- 3.3.需要注意的是:在ClassMomentInfo类中,还有另外一个成员变量mMediaBeans,由于该成员变量存储的是MediaBean类型,所以需要单独创建类似StringTypeConverter的类,且需要在mMediaBeans上方加上“@TypeConverters(MediaBeanTypeConverter.class)”。此时,新建MediaBeanTypeConverter类,代码如下:
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.pj.teacherlocal.entity.work.MediaBean;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.List;
public class MediaBeanTypeConverter {
Gson gson = new Gson();
@TypeConverter
public List<MediaBean> stringToSomeObjectList(String data) {
if (data == null) {
return Collections.emptyList();
}
Type listType = new TypeToken<List<MediaBean>>() {
}.getType();
return gson.fromJson(data, listType);
}
@TypeConverter
public String someObjectListToString(List<MediaBean> someObjects) {
return gson.toJson(someObjects);
}
}
- 3.4.综上,处理方案已经很明显了。
- 四.为了避免有些小伙伴疑惑,我将类ClassMomentInfo和类MediaBean的完整代码贴出来。
ClassMomentInfo
import android.arch.persistence.room.Embedded;
import android.arch.persistence.room.Entity;
import android.arch.persistence.room.PrimaryKey;
import android.arch.persistence.room.TypeConverters;
import com.pj.teacherlocal.entity.CurrentTeacherInfo;
import com.pj.teacherlocal.utils.MediaBeanTypeConverter;
import com.pj.teacherlocal.utils.StringTypeConverter;
import java.util.List;
@Entity(tableName = "classMomentInfo")
public class ClassMomentInfo {
@PrimaryKey(autoGenerate=true)
private long uid;
private String content;
private String date;
//ROOM不支持直接存储集合
@TypeConverters(MediaBeanTypeConverter.class)
private List<MediaBean> mMediaBeans;
@TypeConverters(StringTypeConverter.class)
private List<String> mClazzs;
//关联另外一个对象
@Embedded
private CurrentTeacherInfo mCurrentTeacherInfo;
public long getUid() {
return uid;
}
public void setUid(long uid) {
this.uid = uid;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public List<MediaBean> getMediaBeans() {
return mMediaBeans;
}
public void setMediaBeans(List<MediaBean> mediaBeans) {
mMediaBeans = mediaBeans;
}
public List<String> getClazzs() {
return mClazzs;
}
public void setClazzs(List<String> clazzs) {
mClazzs = clazzs;
}
public CurrentTeacherInfo getCurrentTeacherInfo() {
return mCurrentTeacherInfo;
}
public void setCurrentTeacherInfo(CurrentTeacherInfo currentTeacherInfo) {
mCurrentTeacherInfo = currentTeacherInfo;
}
@Override
public String toString() {
return "ClassMomentInfo{" +
"uid=" + uid +
", content='" + content + '\'' +
", date='" + date + '\'' +
", mMediaBeans=" + mMediaBeans +
", mClazzs=" + mClazzs +
", mCurrentTeacherInfo=" + mCurrentTeacherInfo +
'}';
}
}
MediaBean
public class MediaBean {
private String url;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
@Override
public String toString() {
return "MediaBean{" +
"url='" + url + '\'' +
'}';
}
}
边栏推荐
猜你喜欢
随机推荐
MySQL知识总结 (二) 存储引擎
UIWindow的makeKeyAndVisible不调用rootviewController 的viewDidLoad的问题
绕过正则实现SQL注入
重新学习编程day1 【初始c语言】【c语言编写出计算两个数之和的代码】
2. Basic use RecyclerView
vscode compiles the keil project and burns the program
猜数字游戏,猜错10次关机(srand、rand、time)随机数生成三板斧(详细讲解!不懂问我!)
The Handler you really understand?
数据乱码问题—更改mysql字符编码
Kubernetes架构和组件
宝塔面板搭建小说CMS管理系统源码实测 - ThinkPHP6.0
Win10不能启动WampServer图标呈橘黄色的解决方法
一文带你快速掌握Kotlin核心技能
7.如何给RecyclerView添加Click和LongClick事件
加减法运算及其溢出处理
C语言字符串——关于指针
自定义UDF函数
ABP,kendo后台接口,新增,查询
十分钟带你入门Nodejs
uniapp小程序禁止遮罩弹窗下的页面滚动的完美解决办法









