如何在 Android 上将嵌套列表 <String> 保存在 RoomDB 中
How to save nested List<String> in RoomDB on Android
嘿google有一个使用@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;
}
是否可以在不为其创建额外 class 的情况下保存字符串列表。我想避免我的 Json属性 和房间实体
之间的不一致
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 注释的 class 或其 collection/array。
@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 注释的 class 或其 collection/array。
@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
}
解决方案 道
如果你使用 Kotlin,你应该使用 ArrayList
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertNewItems(itemList: ArrayList<Item>)
我有同样的问题,我用 @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<String>
都将在您的数据库中序列化为 JSONArray。
对于您的数据库 class,即扩展 RoomDatabase
的数据库,您将必须声明要使用哪个 class 与 @TypeConverters(Converters.class)
进行此转换。例如
@Database(version = 1, entities = {Entity.class})
@TypeConverters(Converters.class)
public abstract class MoviesDatabase extends RoomDatabase {
嘿google有一个使用@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;
}
是否可以在不为其创建额外 class 的情况下保存字符串列表。我想避免我的 Json属性 和房间实体
之间的不一致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 注释的 class 或其 collection/array。
@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 注释的 class 或其 collection/array。
@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
}
解决方案 道
如果你使用 Kotlin,你应该使用 ArrayList
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertNewItems(itemList: ArrayList<Item>)
我有同样的问题,我用 @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<String>
都将在您的数据库中序列化为 JSONArray。
对于您的数据库 class,即扩展 RoomDatabase
的数据库,您将必须声明要使用哪个 class 与 @TypeConverters(Converters.class)
进行此转换。例如
@Database(version = 1, entities = {Entity.class})
@TypeConverters(Converters.class)
public abstract class MoviesDatabase extends RoomDatabase {