I try to call fun bind declared in the inner class LaunchesViewHolder from onBindViewHolder() but I got error "Unresolved resource bind" I was trying with an other variable x, just to see, same problem
class LaunchesAdapter(private val dataSet: List<LaunchItem>) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
inner class LaunchesViewHolder( val binding: LaunchesItemLayoutBinding) :
RecyclerView.ViewHolder(binding.root) {
val x = 0
public fun bind(currentLaunch: LaunchItem) {
//do something
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return LaunchesViewHolder(
LaunchesItemLayoutBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
holder.bind(dataSet[position]) => error unresolved resource bind
holder.x =1 => error unresolved resource x
}
override fun getItemCount(): Int {
return dataSet.size
}
}````
CodePudding user response:
In your onBindViewHolder
you should use your specific ViewHolder
, that is LaunchesViewHolder
and not the RecyclerView.ViewHolder
. Please see code below.
override fun onBindViewHolder(holder: LaunchesViewHolder, position: Int) {
holder.bind(dataSet[position])
}
Edited: You need to specify the class you override too
class LaunchesAdapter(private val dataSet: List<LaunchItem>) :
RecyclerView.Adapter<LaunchesAdapter.LaunchesViewHolder>() {
}
CodePudding user response:
it works with (holder as LaunchesViewHolder).bind(dataSet[position]) instead of holder.bind(dataSet[position])
see more details https://www.section.io/engineering-education/implementing-multiple-viewholders-in-android-using-kotlin/