我正在尝试使用以下 TypeConverter
在我的房间数据库中保留一个时间戳:
class Converters {
@TypeConverter
fun fromTimestamp(value: Long?): Calendar? {
if(value == null) return null
val cal = GregorianCalendar()
cal.timeInMillis = value
return cal
}
@TypeConverter
fun toTimestamp(timestamp: Calendar?): Long? {
if(timestamp == null) return null
return timestamp.timeInMillis
}
}
我的两个实体包括以下列 :
@ColumnInfo(name = "timestamp")
val timestamp: Calendar?,
但在尝试构建项目时出现编译错误 - 使用开发人员引用指南中的 Date
TypeConverter
示例时没有问题。
我无法看到实际的错误是什么,因为如果与 Room 相关的代码有问题,我只会收到一堆数据绑定(bind)“找不到符号”错误。
最佳答案
用途:
object Converters {
@TypeConverter
@JvmStatic
fun fromTimestamp(value: Long?): Calendar? = value?.let { value ->
GregorianCalendar().also { calendar ->
calendar.timeInMillis = value
}
}
@TypeConverter
@JvmStatic
fun toTimestamp(timestamp: Calendar?): Long? = timestamp?.timeInMillis
}
和
@TypeConverters(Converters::class)
abstract class AppDatabase : RoomDatabase() {
关于sqlite - 房间的日历类型转换器(Kotlin),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54748483/