更新时间:2023-01-12 08:47:34
最后,我认识到Room需要在类型转换器实现中进行一些特殊更改(更改为ArrayList<HashMap<String, OtherServiceEntity>>
).尽管如此,还是不知道这是一个功能或错误,因为在其他情况下,我仍然可以使用List作为类型.这就是我所做的细节,现在我可以运行它了
Finally, I recognized that Room needs some special change in type converter implementation (change to ArrayList<HashMap<String, OtherServiceEntity>>
). Still, don't know it is a feature or bug because other cases I am still able to use List as type. This is the detail what I did, and now I am able to run it
@Entity(tableName = "ProfileEntities")
data class ProfileEntity(
@TypeConverters(ProfileTypeConverter::class)
@SerializedName("details") var details: List<ProfileDetailEntity> = listOf(),
@Embedded
@SerializedName("images") var images: ProfileImagesEntity = ProfileImagesEntity(),
@PrimaryKey
@SerializedName("masterId") var masterId: Long = 0,
@TypeConverters(OtherServicesTypeConverter::class)
@SerializedName("otherServices") var otherServices: ArrayList<HashMap<String, OtherServiceEntity>> = arrayListOf(),
@SerializedName("updatedAt") var updatedAt: String = ""
)
以及转换器的实现
class ProfileTypeConverter {
private val gson = Gson()
@TypeConverter
fun stringToList(data: String?): List<ProfileDetailEntity> {
if (data == null) {
return Collections.emptyList()
}
val listType = object : TypeToken<List<ProfileDetailEntity>>() {
}.type
return gson.fromJson<List<ProfileDetailEntity>>(data, listType)
}
@TypeConverter
fun listToString(someObjects: List<ProfileDetailEntity>): String {
return gson.toJson(someObjects)
}
}
class OtherServicesTypeConverter {
private val gson = Gson()
@TypeConverter
fun stringToList(data: String?): ArrayList<HashMap<String, OtherServiceEntity>> {
if (data == null) {
return ArrayList()
}
val listType = object : TypeToken<ArrayList<HashMap<String, OtherServiceEntity>>>() {
}.type
return gson.fromJson<ArrayList<HashMap<String, OtherServiceEntity>>>(data, listType)
}
@TypeConverter
fun listToString(objects: ArrayList<HashMap<String, OtherServiceEntity>>): String {
return gson.toJson(objects)
}
}
祝您编程愉快!