【发布时间】:2021-10-13 16:00:21
【问题描述】:
对不起,我的愚蠢问题。
我有谷歌这个问题,但答案对我不起作用。
这是我的代码。
data class Article(val id: Long, val title: String, val ingress: String, val image: String,
val dateTime: String, val tags: List<String>, val content: List<Item>, val created: Long, val changed: Long) {
@delegate:Transient
val formatDateString: String by lazy {
val sdf = SimpleDateFormat("dd.MM.yyyy HH.mm")
val date: Date = sdf.parse(dateTime)
var time: String?
if (date.year == Date().year) {
time = SimpleDateFormat("dd MM,HH:mm", Locale.US).format(date)
} else {
time = SimpleDateFormat("dd MM yyyy,HH:mm", Locale.US).format(date)
}
time!!
}
}
当我尝试获取 formatDateString 值时,会发生上述异常。
访问formatDateString的代码如下
override fun getCustomAdapter(): BaseQuickAdapter<CarListResponseBody.Article, BaseViewHolder> {
return object : BaseQuickAdapter<CarListResponseBody.Article, BaseViewHolder>( R.layout.app_item_cars_list,dataList) {
override fun convert(helper: BaseViewHolder, item: CarListResponseBody.Article) {
helper.setText(R.id.tv_ingress,item.ingress)
helper.setText(R.id.tv_date_time,item.formatDateString)
helper.setText(R.id.tv_title,item.title)
ImageLoadUtil.loadImgToView(context,item.image,helper.getView(R.id.img_car),null)
}
}
}
我认为这个异常是由 Gson 引起的,但我不知道为什么
【问题讨论】: