如何在Android上的RoomDB中保存嵌套的List <String>

mur*_*urt 1 sqlite android android-room

嘿谷歌有一个使用@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;
 }
Run Code Online (Sandbox Code Playgroud)

是否可以保存String列表而不为其创建额外的类.我想避免我的JsonProperty和房间实体之间的混淆

W想拥有那样的东西

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

因为我收到以下Json:

{ 
 "id" : "1",
"name" : "someName",
"pets": ["cat", "dog", "camel"]
}
Run Code Online (Sandbox Code Playgroud)

谁知道解决方案?

编辑:

现在我的示例代码看起来像但我有错误: 错误:参数的类型必须是使用@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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

EDIT2

节能项目时道不能插入我的实体的名单新出现的问题,没有理由为什么...虽然 错误:参数的类型必须与@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

}
Run Code Online (Sandbox Code Playgroud)

道的解决方案

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

@Insert(onConflict = OnConflictStrategy.REPLACE)
        fun insertNewItems(itemList: ArrayList<Item>)
Run Code Online (Sandbox Code Playgroud)

Bla*_*elt 6

我有同样的问题,我解决了@TypedConverter.我正在保存列表JSONArray.toString中的db.

@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);
}
Run Code Online (Sandbox Code Playgroud)

这样每个都List<String>将被序列化为数据库中的JSONArray.

对于扩展的db类,RoomDatabase您必须声明要用于此转换的类@TypeConverters(Converters.class).例如

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