在转换ArrayList <float>时,参数的类型必须是带有@Entity或其集合/数组的类。

时间:2019-01-20 16:39:49

标签: database arraylist kotlin typeconverter

当我尝试将ArrayList<Float>另存为会议室数据库中的列时出错

   @Entity(tableName = "Height")
class Height: Serializable {

  @PrimaryKey(autoGenerate = true)
  var id: Long = 0

  @ColumnInfo(name = "data")
  var data: ArrayList<Float>? = ArrayList()
}
@Dao
abstract class HeightDao : BaseDao<Height>() {

  @Insert(onConflict = OnConflictStrategy.REPLACE)
  abstract fun insertAllHeight(listOfHeight: ArrayList<Float>)

我写了TypeConverter

    @TypeConverter
fun fromArrayListOfFloats(list: ArrayList<Float>?): String {
    return list?.joinToString(separator = ";") { it.toString() } ?: ""
}

    @TypeConverter
fun toArrayListOfFloats(string: String?): ArrayList<Float> {
    return ArrayList(string?.split(";")?.mapNotNull { it.toFloatOrNull() } ?: emptyList())
}

当我收敛ArrayList<SomeEntityObject>时,它工作正常,但是在这种情况下,会出现上述错误。请帮忙!

0 个答案:

没有答案