Android中在使用Room时提示:Cannot figure out how to save this field into database. You can consider adding a
场景
在Android中使用Room进行存储数据库时提示:
Cannot figure out how to save this field into database. You can consider adding a type converter for
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。
实现
这是因为Room中不支持对象中直接存储集合。
下面是要存储的对象Bean
@Entity
public class ChatBean {private String msg;private int code;@NonNull@PrimaryKeyprivate String id = "";private List<ChatItem> data;@Entitypublic static class ChatItem {@PrimaryKeyprivate int id;private String msgNum;private String content;//语音消息服务器地址private String remoteContent;private String sender;private String receiver;private String type;private boolean canReceived;private String sendTime;private String receivedTime;//语音时长private int voiceDuration;private boolean isRead;}}
上面省略了get和set方法,在bean中还有个 对象集合data,对象为ChatItem
所以需要新建一个转换类ChatItemConverter
名字根据自己业务去定
package com.bdtd.bdcar.database;import androidx.room.TypeConverter;import com.bdtd.bdcar.bean.ChatBean;
import com.bdtd.bdcar.common.GsonInstance;
import com.google.gson.reflect.TypeToken;import java.lang.reflect.Type;
import java.util.List;public class ChatItemConverter {@TypeConverterpublic String objectToString(List<ChatBean.ChatItem> list) {return GsonInstance.getInstance().getGson().toJson(list);}@TypeConverterpublic List<ChatBean.ChatItem> stringToObject(String json) {Type listType = new TypeToken<List<ChatBean.ChatItem>>(){}.getType();return GsonInstance.getInstance().getGson().fromJson(json, listType);}
}
此转换类的功能是实现对象与json数据的转换。
为了使用方便,这里将gson抽离出单例模式
所以新建GsonInstance
package com.bdtd.bdcar.common;import com.google.gson.Gson;public class GsonInstance {private static GsonInstance INSTANCE;private static Gson gson;public static GsonInstance getInstance() {if (INSTANCE == null) {synchronized (GsonInstance.class) {if (INSTANCE == null) {INSTANCE = new GsonInstance();}}}return INSTANCE;}public Gson getGson() {if (gson == null) {synchronized (GsonInstance.class) {if (gson == null) {gson = new Gson();}}}return gson;}}
然后转换类新建完成。
在上面的实体bean,ChatBean上面添加注解。
@TypeConverters(ChatItemConverter.class)
Android中在使用Room时提示:Cannot figure out how to save this field into database. You can consider adding a相关推荐
- IDEA中启动SpringBoot项目时提示:“Error java:程序包xxx不存在”,但实际上是存在的
场景 在IDEA中启动SpringBoot项目时提示: Error java: 程序包xxx不存在" 但是实际上这个包是存在的. 这个包是自己写的websocket的服务端和客户端的包,是存 ...
- ASP.NET中连接Sqlserver数据库时提示:未能加载文件或程序集“Microsoft.SqlServer.Management.Sdk.Sfc
场景 在Visual Studio 2013中连接 SqlServer数据库时提示: 未能加载文件或程序集"Microsoft.SqlServer.Management.Sdk.Sfc 注: ...
- Ubuntu中重启ssh服务时提示:Job for ssh.service failed because the control process exited with error code. See
场景 在Ubuntu中重启ssh服务时提示: Job for ssh.service failed because the control process exited with error code ...
- IDEA中进行SpringBoot开发时提示:Error resolving template xxx, template might not exist or might not be acce
场景 IDEA中进行SpringBoot开发时提示: Error resolving template "xxx", template might not exist or mig ...
- android listview 数据同步,android中ListView数据刷新时的同步方法
本文实例讲述了android中ListView数据刷新时的同步方法.分享给大家供大家参考.具体实现方法如下: public class Main extends BaseActivity { priv ...
- TP框架中ajax post请求时提示404
ajax post请求时提示404错误 TP框架中ajax post请求时提示404 找了半天是 控制器中方法有错误! 下次再遇到去控制器方法中找一步一步找,肯定能找到,我是单词拼错了! 转载于:ht ...
- android中的横竖屏切换,Android中横竖屏切换时Activity的生命周期
Android中横竖屏切换时Activity的生命周期执行情况 1.默认情况下生命周期 (1)第一次进入界面 11-17 13:55:18.452: E/ImageListActivity(10586 ...
- 此程序将从您的计算机删除adobe,电脑中安装Adobe软件时提示该程序已安装的最佳解决方案...
很多用户都喜欢在电脑中安装这款Adobe软件来使用,不过有时候会碰到一些故障,就有用户在电脑中安装Adobe软件的时候,却提示该程序已安装或者无法安装的现象,这是怎么回事呢,经过分析一般是之前安装过没 ...
- Android问题-DelphiXE5编义时提示找不到“连接器(arm-linux-androideabi-ld.exe)
问题现象:DelphiXE5编义时提示找不到"连接器(arm-linux-androideabi-ld.exe)" 问题提示: Checking project dependenc ...
最新文章
- 大赛季(2020)新一代信息技术行业赛赛事公告
- 1.虚拟化的历史和定义
- 通信经典书籍(转载)
- Eclipse中新建SpringBoot项目并输出HelloWorld
- Android Shell命令dumpsys
- html引入百度地图报错,vue引入百度地图BMapGL,或者其他个性化地图
- PyQt5笔记(04) -- 文本框的使用
- stm32中.bss和.data段是在哪里初始化的
- 【转】android gravity属性 和 weight属性
- 2007以学习软件设计开始
- 栈的应用-后缀表达式
- switchhosts以管理员身份运行后不显示故障处理
- fork函数结果分析
- 拓扑排序 java_[Java]聊聊拓扑排序算法
- html怎样在雪景里插入文字,配雪景的唯美文字
- 0x300-从头开始写操作系统-内核
- Last Corollary CodeForces - 1364D(dfs树找最小环)
- mysql如何加索引的时候不锁表
- 1.41亿老人遭受过虐待,厌老憎老为何成了时代病?
- admi后台 vue_vue-admin
热门文章
- linux下的ppp软件,linux下ppp拨号无线上网(示例代码)
- php和java语言_JAVA语言和PHP语言的比较
- 安装计算机教室的请示,教室使用申请书4篇
- 源达投顾软件怎么样_源达投顾:让人工智能炒股终端变得更简单
- 用python定义一个员工类_python类的定义和使用
- qt框架的开发模式_Flutter 混合开发框架模式探索
- oracle转mysql数据库
- win10 远程桌面无法连接报错
- Linux关闭开关机动画,centos7删除开机动画及修改启动菜单
- 电脑开机3秒就重启循环_移动办公神器!电脑包秒变支架、鼠标垫,3合1设计超方便!...