【发布时间】:2021-02-12 11:48:39
【问题描述】:
我有一个回收站视图 ListAdapter(来自 androidx)
我知道不应再将 notifyDataSetChanged() 与 ListAdaper 一起使用,但我处于必要的情况(应在不更改提交列表的情况下调用 onBindViewHolder())
问题是当我像这样调用 notifyDataSetChanged() 时出现编译错误:
binding.episodesRv.adapter.notifyDataSetChanged()
编译错误:
Smart cast to '({Adapter<(androidx.recyclerview.widget.RecyclerView.ViewHolder..
androidx.recyclerview.widget.RecyclerView.ViewHolder?)> & EpisodeListAdapter}..
EpisodeListAdapter)' is impossible, because 'bd.episodesRv.adapter' is a complex expression
适配器:
class EpisodeListAdapter(
val context: Context,
var epiId: StateFlow<Long>,
val selectEpisode: (episode: Episode) -> Unit,
val delete: (episode: Episode) -> Unit,
) : ListAdapter<Episode, EpisodeViewHolder>(EPISODE_COMPARATOR) {
....
【问题讨论】:
-
它可能会发生,如果您尝试从对象中获取某些内容,此时无论出于何种原因,它都可能为 null,并且编译器无法执行智能转换。在您的情况下,它可能是一个适配器。如果您确定该适配器在调用时不为空,则可以尝试对诸如 binding.episodesRv.adapter!!.notifyDataSetChanged() 之类的调用进行非空断言。
-
谢谢阿特姆。 bd.episodesRv.adapter?.notifyDataSetChanged() 工作正常。问题已解决
标签: android android-recyclerview android-listadapter