DBFlow版本:4.0.4 嗨,我正在使用dbflow Android ORM和Kotlin的List Type转换器。我有一个像这样定义的数据类:
@Table(database = StopsDb::class)
data class FavouriteStop(
@PrimaryKey @Column var id: String = "",
@Index @Column var name: String = "",
@Column(typeConverter = StringListConverter::class) var directions: List<String> = listOf(),
@Column(typeConverter = StringListConverter::class) var selectedDirections: List<String> = listOf()
) : BaseRXModel()
因为我不想创建一个单独的表来存储字符串我创建了一个List Type转换器,如下所示:
class StringListConverter : TypeConverter<String, List<String>>() {
val separator = ","
override fun getDBValue(model: List<String>?): String {
if(model==null || model.isEmpty())
return ""
else
return model.joinToString (separator = separator){ it }
}
override fun getModelValue(data: String?): List<String> {
return data?.split(separator) ?: listOf()
}
}
但是在构建阶段会抛出以下错误:
Error:error: *==========* :The specified custom TypeConverter's Model Value java.util.List<? extends java.lang.String> from com.kapuscinski.departures.persistence.db.StringListConverter must match the type of the column java.util.List<java.lang.String>.*==========*
我在这里遗漏了什么,以及如何解决这个问题?在此先感谢您的帮助!
答案 0 :(得分:2)
将所有内容从“列表”更改为“MutableList”
@Table(database = StopsDb::class)
data class FavouriteStop(
@PrimaryKey @Column var id: String = "",
@Index @Column var name: String = "",
@Column(typeConverter = StringListConverter::class) var directions: MutableList<String> = mutableListOf(),
@Column(typeConverter = StringListConverter::class) var selectedDirections: MutableList<String> = mutableListOf()
) : BaseRXModel()
class StringListConverter : TypeConverter<String, MutableList<String>>() {
val separator = ","
override fun getDBValue(model: MutableList<String>?): String =
if (model == null || model.isEmpty())
""
else
model.joinToString(separator = separator) { it }
override fun getModelValue(data: String?): MutableList<String> {
return data?.split(separator)?.toMutableList() ?: mutableListOf()
}
}