无法将列表转换为 Kotlin 中的特定数据 class 到 %Class%

Unable to cast a list to a specific data class in Kotlin to %Class%

我的数据class:

import android.os.Parcel
import android.os.Parcelable

data class Product(
    var name: String? = " ",
    var price:Long = 0,
    var Image: String? = " "
):Parcelable {
    constructor(parcel: Parcel) : this(
        parcel.readString(),
        parcel.readLong(),
        parcel.readString()
    ) {
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(name)
        parcel.writeLong(price)
        parcel.writeString(Image)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<Product> {
        override fun createFromParcel(parcel: Parcel): Product {
            return Product(parcel)
        }

        override fun newArray(size: Int): Array<Product?> {
            return arrayOfNulls(size)
        }
    }
}

我试图在 class 中声明一个列表。 Class 是:

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Filter
import android.widget.Filterable
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.demoshop.Activity.MainActivity
import com.example.demoshop.Model.Product
import com.example.demoshop.R
import kotlinx.android.synthetic.main.single_item.view.*
import java.util.*

class ProductAdapter(
    private val products:MutableList<Product>,
    private val context:Context)
    : RecyclerView.Adapter<MainActivity.ProductViewHolder>(), Filterable {
    var filteredProduct :MutableList<Product> = products


    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): MainActivity.ProductViewHolder {
        val view: View = LayoutInflater.from(parent.context).
        inflate(
            R.layout.single_item,
            parent,
            false
        )
        return MainActivity.ProductViewHolder(view)
    }

    override fun onBindViewHolder(holder: MainActivity.ProductViewHolder, position: Int) {

        holder.itemView.tittle.text = filteredProduct[position].name
        holder.itemView.product_price.text = filteredProduct[position].price.toString()
        Glide
            .with(context)
            .load(filteredProduct[position].Image)
            .into(holder.itemView.product_image);

    }

    override fun getItemCount(): Int {
        return filteredProduct.size
    }


    override fun getFilter(): Filter {
        return object : Filter() {
            override fun performFiltering(constraint: CharSequence?): FilterResults {
                val pattern: String = constraint.toString().toLowerCase(Locale.ROOT)
                var filteredList = mutableListOf<Product>()
                if (pattern.isEmpty() || pattern==null) {
                    filteredList = products
                } else {
                    for (p: Product in products) {
                        if (p.name!!.toLowerCase(Locale.ROOT).contains(pattern)) {
                            filteredList.add(p)
                        }
                    }
                }
                val filterResults = FilterResults()
                filterResults.values = filteredList

                return filterResults

            }

            override fun publishResults(
                constraint: CharSequence?,
                results: FilterResults?
            ) {
                filteredProduct.clear()
                filteredProduct = mutableListOf(results!!.values) as MutableList<Product>
                notifyDataSetChanged()
            }

        }
    }

}

我的 logcat :

2021-01-04 09:36:34.895 17122-17122/com.example.demoshop E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.demoshop, PID: 17122
    java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.example.demoshop.Model.Product
        at com.example.demoshop.adapter.ProductAdapter.onBindViewHolder(productAdapter.kt:39)
        at com.example.demoshop.adapter.ProductAdapter.onBindViewHolder(productAdapter.kt:17)

发生崩溃是因为您试图将 List<List<Product>> 转换为 List<Product>。调用 mutableListOf(results!!.values) 会创建一个额外的包装列表。

修改代码如下:

override fun publishResults(
                constraint: CharSequence?,
                results: FilterResults?
            ) {
                filteredProduct.clear()
                filteredProduct = results!!.values as MutableList<Product>
                notifyDataSetChanged()
            }