【发布时间】:2018-10-31 05:24:00
【问题描述】:
我正在使用 Room,这是我的数据类
@Entity(tableName = "buy_lead")
class BuyLead(
@PrimaryKey
@SerializedName("id")
val id: String,
@SerializedName("name")
val name: String,
@SerializedName("location")
val location: String,
@SerializedName("state")
val state: String?,
@SerializedName("message")
val message: String,
@SerializedName("product_service")
val serviceType: String?,
@SerializedName("posted_on")
val postedOn: String,
@SerializedName("formatted_date")
val formattedDate: String,
@SerializedName("seconds")
val seconds: Int,
@SerializedName("minutes")
val minutes: Int,
@SerializedName("hours")
val hours: Int,
@SerializedName("days")
val days: Int,
var bought: Boolean = false,
@Ignore var error: String? = null
) {
val postTime: String
get() {
if (seconds < 60) {
if (seconds == 1)
return "$seconds second ago"
return "$seconds seconds ago"
}
if (minutes < 60) {
if (minutes == 1)
return "$minutes minute ago"
return "$minutes minutes ago"
}
if (hours < 24) {
if (hours == 1)
return "$hours hour ago"
return "$hours hours ago"
}
if (days < 5) {
if (days == 1)
return "$days day ago"
return "$days days ago"
}
return formattedDate
}
val address: String
get() =
if (state != null) "${location}, ${state}"
else location
}
它会产生编译时错误
错误:实体和 Pojos 必须有一个可用的公共构造函数。您可以有一个空的构造函数或参数与字段匹配的构造函数(按名称和类型)。 公共最终类 BuyLead { ^
我试图移动类内的字段,但没有任何效果。如果我从错误中删除 @Ignore 注释,它就会编译。我不想在数据库中存储错误字段。
Kotlin 版本是 1.2.71
【问题讨论】:
标签: android kotlin android-room