【问题标题】:How to save nested List<String> in RoomDB on Android如何在 Android 上的 RoomDB 中保存嵌套的 List<String>
【发布时间】:2017-08-03 07:42:50
【问题描述】:

嘿谷歌有一个使用@Relation的例子

@Entity
 public class Pet {
     int userId;
     String name;
     // other fields
 }
 public class UserNameAndAllPets {
   public int id;
   public String name;
   @Relation(parentColumn = "id", entityColumn = "userId")
   public List<Pet> pets;
 }

是否可以在不为其创建额外类的情况下保存字符串列表。我想避免我的 JsonProperty 和房间实体之间的不一致

W想要这样的东西

 public class UserNameAndAllPets {
   @JsonProperty("id")
   public int id;
   @JsonProperty("name")
   public String name;
   @Relation(parentColumn = "id")
   @JsonProperty("pets")
   public List<String> pets;
 }

因为我收到以下 Json:

{ 
 "id" : "1",
"name" : "someName",
"pets": ["cat", "dog", "camel"]
}

有人知道解决办法吗?

编辑:

现在我的示例代码看起来像但是我有错误: 错误:参数的类型必须是带有@Entity 注释的类或其集合/数组。

@JsonIgnoreProperties(ignoreUnknown=true)
@Entity(tableName = TABLE_NAME)
public class Item {
    @Ignore public static final String TABLE_NAME = "itemTable";

    @PrimaryKey(autoGenerate = true)
    Long id;
    @JsonProperty("supplierName")
    String supplierName;
    @JsonProperty("eventDescription")
    String eventDescription;
    @JsonProperty("eventDate")
    @TypeConverters(StringListToGsonConverter.class)
    Date date;
    @JsonProperty("carServiceType")
    @TypeConverters(StringListToGsonConverter.class)
    List<String> types;

    public ServiceHistoryItem(Long id, String supplierName, String eventDescription, Date date, List<String> types) {
        this.id = id;
        this.supplierName = supplierName;
        this.eventDescription = eventDescription;
        this.date = date;
        this.types = types;
    }

    public static class StringListToGsonConverter{
        @TypeConverter
        public static List<String> restoreList(String listOfString){
            return new Gson().fromJson(listOfString, new TypeToken<List<String>>() {}.getType());
        }

        @TypeConverter
        public static String saveListOfString(List<String> listOfString){
            return new Gson().toJson(listOfString);
        }

        @TypeConverter
        public static Date fromTimestamp(Long value) {
            return value == null ? null : new Date(value);
        }

        @TypeConverter
        public static Long dateToTimestamp(Date date) {
            return date == null ? null : date.getTime();
        }
    }
}

EDIT2

保存物品时出现新问题 Dao 无法插入我的实体列表,没有理由为什么......虽然 错误:参数的类型必须是带有@Entity 注释的类或其集合/数组。

@Dao
interface ItemDao {
    @Query("SELECT * FROM " + Item.TABLE_NAME)
    fun getAll(): LiveData<List<Item>>

    @Query("DELETE FROM " + Item.TABLE_NAME)
    fun deleteAllServiceHistory()

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertNewItem(item: Item)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertNewItems(itemList: List<Item>) // <--- Error

}

解决方案 for Dao

如果你使用 Kotlin,你应该使用 ArrayList

@Insert(onConflict = OnConflictStrategy.REPLACE)
        fun insertNewItems(itemList: ArrayList<Item>)

【问题讨论】:

    标签: android sqlite android-room


    【解决方案1】:

    我遇到了同样的问题,我用@TypedConverter 解决了这个问题。我在db 中将列表另存为JSONArray.toString

    @TypeConverter
    public static List<String> restoreList(String listOfString) {
        return new Gson().fromJson(listOfString, new TypeToken<List<String>>() {}.getType());
    }
    
    @TypeConverter
    public static String saveList(List<String> listOfString) {
        return new Gson().toJson(listOfString);
    }
    

    这样每个List&lt;String&gt; 都会在你的数据库中被序列化为一个 JSONArray。

    对于扩展RoomDatabase 的数据库类,您必须使用@TypeConverters(Converters.class) 声明用于此转换的类。例如

    @Database(version = 1, entities = {Entity.class})
    @TypeConverters(Converters.class)
    public abstract class MoviesDatabase extends RoomDatabase {
    

    【讨论】:

    • 对于@TypeConverters(StringListToGsonConverter.class) List&lt;String&gt; someType = null; 我得到了错误:参数的类型必须是用@Entity 注释的类或其集合/数组。
    • 您能用您所做的更改更新代码吗?
    • 我从未在实体字段上使用过@TypeConverters(StringListToGsonConverter.class)。根据文档,它应该是可能的。这可能是一个错误。尝试在实体级别或数据库级别上移动它。
    • 答案是DAO无法保存List
    猜你喜欢
    • 2011-02-22
    • 2013-10-04
    • 2020-10-20
    • 2021-06-15
    • 1970-01-01
    • 1970-01-01
    • 2015-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多