当前位置:网站首页>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 + '\'' +
'}';
}
}
边栏推荐
猜你喜欢
随机推荐
主存储器(一)
spark优化
binlog与iptables防nmap扫描
UIWindow的makeKeyAndVisible不调用rootviewController 的viewDidLoad的问题
关于UDF
每周招聘|PostgreSQL专家,年薪60+,高能力高薪资
【c】大学生在校学习c语言常见代码
7.如何给RecyclerView添加Click和LongClick事件
ThinkPHP5.0内置分页函数Paginate无法获取POST页数问题的解决办法
无人驾驶综述:等级划分
原码、补码、反码
我理解的学习金字塔
再见篇:App专项技术优化
c语言三子棋详解!!! (电脑智能下棋)(附上完整代码)
执行栈和执行上下文
原码、反码、补码和移码
一文带你快速掌握Kotlin核心技能
2.RecyclerView基本使用
Redis数据库相关指令
mysql