在BindViewHolder中添加ViewHolder类时,覆盖函数不会覆盖。当我把viewHolder类放在它外面时,它不起作用。
下面是我的代码-
class data_custom_adapter(private val context: Context, private val datalist : ArrayList<Display_data>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomAdapter.ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.display_data_card, parent, false)
return CustomAdapter.ViewHolder(v)
}
//Issue occur here -- it is not overriding when I add ViewHolder class
override fun onBindViewHolder(holder: data_custom_adapter.ViewHolder, position: Int) {
holder.bindItems(context,datalist[position])
}
override fun getItemCount(): Int {
return datalist.size
}
//the class is holding the list view
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val datalist_data = itemView.findViewById(R.id.displayxmldata) as TextView
fun bindItems(context: Context, datashow : Display_data) {
datalist_data.text = datashow.appdata
}
}
}
伙计们,请帮忙找出这个问题。
ViewHolder必须与您在第一行中指示的类匹配。所以在中
class data_custom_adapter(private val context: Context, private val datalist : ArrayList<Display_data>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
存在CCD_ 1。将其更改为您的data_custom_adapter.ViewHolder
,它应该可以工作。就像
class data_custom_adapter(private val context: Context, private val datalist : ArrayList<Display_data>) : RecyclerView.Adapter<data_custom_adapter.ViewHolder>() {
这也需要在onCreateViewHolder
功能中进行更改
我希望这能奏效。
class data_custom_adapter(
private val context: Context,
private val datalist : ArrayList<Display_data>) : RecyclerView.Adapter<data_custom_adapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.display_data_card, parent, false)
return data_custom_adapter.ViewHolder(v)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bindItems(context,datalist[position])
}
override fun getItemCount(): Int {
return datalist.size
}
//the class is holding the list view
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val datalist_data = itemView.findViewById(R.id.displayxmldata) as TextView
fun bindItems(context: Context, datashow : Display_data) {
datalist_data.text = datashow.appdata
}
}
}