gpt4 book ai didi

android - null 不能转换为非 null 类型 kotlin.collections.List - kotlin

转载 作者:行者123 更新时间:2023-11-30 04:55:02 24 4
gpt4 key购买 nike

我正在尝试在 RecyclerView 中添加搜索 Filterable。但应用程序因以下错误而崩溃:

kotlin.TypeCastException: null cannot be cast to non-null type kotlin.collections.List at com.kabelash.movies.ui.PostListAdapter$searchFilter$1.publishResults(PostListAdapter.kt:87) at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:284) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:214)

适配器:

class PostListAdapter: RecyclerView.Adapter<PostListAdapter.ViewHolder>(), Filterable {
private lateinit var postListFiltered:List<Data>
private lateinit var postListFull:List<Data>

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val binding: ListItemBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.context), R.layout.list_item, parent, false)
return ViewHolder(binding)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(postListFiltered[position])
}

override fun getItemCount(): Int {
return if(::postListFiltered.isInitialized) postListFiltered.size else 0
}

fun updatePostList(postList:List<Data>){
this.postListFiltered = postList
notifyDataSetChanged()
}

class ViewHolder(private val binding: ListItemBinding): RecyclerView.ViewHolder(binding.root){
private val viewModel = MoviesViewModel()

fun bind(post:Data){
viewModel.bind(post)
binding.viewModel = viewModel
}

}

override fun getFilter(): Filter? {
return searchFilter
}

private val searchFilter: Filter = object : Filter() {
override fun performFiltering(constraint: CharSequence?): FilterResults? {
val filteredList: MutableList<Data> = ArrayList()
if (constraint == null || constraint.isEmpty()) {
filteredList.addAll(postListFull)
} else {
val filterPattern = constraint.toString().toLowerCase().trim()
for (item in postListFull) {
if (item.title.toLowerCase().contains(filterPattern) || item.genre.toLowerCase().contains(filterPattern)) {
filteredList.add(item)
}
}

}
val results = FilterResults()
results.values = filteredList
return results
}

override fun publishResults(constraint: CharSequence?,results: FilterResults) {
postListFiltered = (results.values as List<Data>)
notifyDataSetChanged()
}
}
}

如果您需要任何其他类文件,请告诉我。有人可以帮我解决吗?

最佳答案

就是这一行: results.values as List<Data>

results.values为空,而您告诉它是一个非空列表,它不能这样做。

关于android - null 不能转换为非 null 类型 kotlin.collections.List - kotlin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59359853/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com