Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 views
in Technique[技术] by (71.8m points)

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

My data 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)
        }
    }
}

I tried to declear a list in class. Class is :

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()
            }

        }
    }

}

my 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)
question from:https://stackoverflow.com/questions/65557755/unable-to-cast-a-list-to-a-specific-data-class-in-kotlin-to-class

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The crash occurs because you are trying to cast a List<List<Product>> to List<Product>. Calling mutableListOf(results!!.values) creates an extra wrapping list.

Change the code as follows:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...