在我的RecyclerView列表的适配器中,出现与投射相关的警告 . as List<Product>
在 myList.addAll(results.values as List<Product>)
中返回以下警告 . 如何在不抑制任何警告的情况下解决这个问题?
未经检查的演员:任何!列表
'Product' class
class Product {
var productName: String? = null
var productDescription: String? = null
constructor() {}
constructor(productName: String, productDescription: String) {
this.productName = productName
this.productDescription = productDescription
}
}
adapter class
class MyListAdapter(private val mCtx: Context, private val myList: MutableList<Product>, private val mTwoPane: Boolean) : RecyclerView.Adapter<MyViewHolder>(), Filterable {
private var myListFull = myList.toMutableList()
private val myFilter = object : Filter() {
override fun performFiltering(constraint: CharSequence?): Filter.FilterResults {
val filteredList = ArrayList<Product>()
when {
constraint == null || constraint.isEmpty() -> filteredList.addAll(myListFull)
else -> {
val filterPattern = constraint.toString().toLowerCase().trim { it <= ' ' }
for (item in myListFull) {
when {
item.productName!!.toLowerCase().contains(filterPattern) -> filteredList.add(item)
}
}
}
}
val results = Filter.FilterResults()
results.values = filteredList
return results
}
override fun publishResults(constraint: CharSequence, results: Filter.FilterResults) {
myList.clear()
myList.addAll(results.values as List<Product>)
notifyDataSetChanged()
}
}
inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var tvTitle: TextView = itemView.findViewById(R.id.rvitem_title)
var tvSubtitle: TextView = itemView.findViewById(R.id.rvitem_subtitle)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val inflater = LayoutInflater.from(mCtx)
val v = inflater.inflate(R.layout.listitem_dualline, parent, false)
return MyViewHolder(v)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val product = myList[holder.adapterPosition]
holder.textviewTitle.text = product.productName
holder.textviewSubtitle.text = product.productDescription
}
override fun getItemCount(): Int {
return myList.size
}
override fun getFilter(): Filter {
return myFilter
}
}