zoukankan      html  css  js  c++  java
  • Android中使用Room时怎样存储带list集合的对象

    场景

    Android中使用Room(ORM关系映射框架)对sqllite数据库进行增删改查:

    https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/110821348

    上面讲了使用Room对简单的对象进行存取到数据库。

    但是如果对象中含有list集合属性,形如下面这种

    @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

    但是Room中不支持对象中直接存储集合。

    注:

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

    实现

    所以需要新建一个转换类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)

    博客园: https://www.cnblogs.com/badaoliumangqizhi/ 关注公众号 霸道的程序猿 获取编程相关电子书、教程推送与免费下载。
  • 相关阅读:
    安卓自动化测试添加用例执行回放
    【十二省2019】异或粽子
    【BZOJ4260】Codechef REBXOR
    【JSOI2015】字符串树
    【HAOI2017】供给侧改革
    【NOI2018】你的名字
    【十二省2019】字符串问题
    【LOJ#6041】事情的相似度
    【SP8093】JZPGYZ
    【BZOJ1396】识别子串
  • 原文地址:https://www.cnblogs.com/badaoliumangqizhi/p/14142450.html
Copyright © 2011-2022 走看看