android - Anroid RecyclerView 中的数据绑定不起作用(Kotlin)
问题描述
这是我的 OnBindViewHolder 函数代码:
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val country = countries[position]
var countryInfoCardBinding = DataBindingUtil.setContentView<CountryInfoCardBinding>(context as Activity, R.layout.country_info_card)
countryInfoCardBinding.country = country
}
这是我的 XML 文件:
解决方案
您应该在 中膨胀数据绑定对象onCreateViewHolder
,而不是onBindViewHolder
. 现在您正在膨胀一个与您的视图无关的对象,这就是为什么(我假设)什么都没有出现
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val country = countries[position]
holder.binding.country = country
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val binding = CountryInfoCardBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
return ViewHolder(binding)
}
inner class ViewHolder(val binding: CountryInfoCardBinding) : RecyclerView.Viewholder(binding.root)
推荐阅读
- sql - 将多行汇总为一个 postgresql
- postgresql - 如何正确地将 Postgrex.Notifications 传递给主管
- powershell - 使用 Powershell 在 DCOM 上授予本地组 (IIS_IUSRS) 访问权限
- java - 如何从调用同一变量的两个线程中获得相同的值?
- java - 使用“?:”模拟短路评估
- java - 保存多对多实体的问题
- css - Wordpress ACF Repeater 背景图片只加载最后一张图片
- powershell - 使用 Power Shell 而不是 PNP 的网站集中特定用户的权限报告
- php - 执行 db:seed Artisan 命令时出现错误“找不到类 'ZipArchive'”
- java - 如果你从标签创建一个 git 特性分支而不是发布分支,会发生什么?