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,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

场景

在Android中使用Room进行存储数据库时提示:

Cannot figure out how to save this field into database. You can consider adding a type converter for

 

Android中在使用Room时提示:Cannot figure out how to save this field into database. You can consider adding a

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

这是因为Room中不支持对象中直接存储集合。

下面是要存储的对象Bean

@Entity
public class ChatBean {
    private String msg;
    private int code;
    @NonNull
    @PrimaryKey
    private String id = "";
    private List<ChatItem> data;

    @Entity
    public static class ChatItem {

        @PrimaryKey
        private 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 {

    @TypeConverter
    public String objectToString(List<ChatBean.ChatItem> list) {
        return GsonInstance.getInstance().getGson().toJson(list);
    }

    @TypeConverter
    public 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

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/136449.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!