android 存储一个list,如何在Android上的RoomDB中保存嵌套的List <String>

嘿谷歌有一个使用@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 pets;

}

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

W想拥有那样的东西

public class UserNameAndAllPets {

@JsonProperty("id")

public int id;

@JsonProperty("name")

public String name;

@Relation(parentColumn = "id")

@JsonProperty("pets")

public List pets;

}

因为我收到以下Json:

{

"id" : "1",

"name" : "someName",

"pets": ["cat", "dog", "camel"]

}

谁知道解决方案?

EDIT:

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

public ServiceHistoryItem(Long id, String supplierName, String eventDescription, Date date, List types) {

this.id = id;

this.supplierName = supplierName;

this.eventDescription = eventDescription;

this.date = date;

this.types = types;

}

public static class StringListToGsonConverter{

@TypeConverter

public static List restoreList(String listOfString){

return new Gson().fromJson(listOfString, new TypeToken>() {}.getType());

}

@TypeConverter

public static String saveListOfString(List 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>

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

fun deleteAllServiceHistory()

@Insert(onConflict = OnConflictStrategy.REPLACE)

fun insertNewItem(item: Item)

@Insert(onConflict = OnConflictStrategy.REPLACE)

fun insertNewItems(itemList: List) //

}

SOLUTION 为道

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

@Insert(onConflict = OnConflictStrategy.REPLACE)

fun insertNewItems(itemList: ArrayList)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值