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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…