1

DBFlow 版本:4.0.4 嗨,我正在努力使用带有 dbflow Android ORM 和 Kotlin 的列表类型转换器。我有一个这样定义的数据类:

@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()

因为我不想创建一个单独的表来存储字符串,所以我创建了一个这样的列表类型转换器:

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>.*==========*

我在这里遗漏了什么,以及如何解决这个问题?提前感谢您的帮助!

4

1 回答 1

2

将所有内容从“列表”更改为“可变列表”

@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()
    }
}
于 2017-12-06T19:37:40.937 回答