kyleg
02/07/2020, 8:46 PMTypeMismatch
in the onCreateViewHolder
for both branches of the when
?
Return type is listed as the sealed class BaseHolder<PossibleRow>
and the return types are both subclasses of the sealed class.
sealed class PossibleRow { /* objects A, B defined here */ }
sealed class BaseHolder<T: PossibleRow>(...): RecyclerView.ViewHolder(...) {
class AHolder(...): BaseHolder<PossibleRow.A>(...)
class BHolder(...): BaseHolder<PossibleRow.B>(...)
}
class LiftGroupRowAdapter {
override fun onCreateViewHolder(..., viewType: Int): BaseHolder<PossibleRow> {
return when(viewType) {
0 -> BaseHolder.AHolder(...)
1 -> BaseHolder.BHolder(...)
else -> throw IllegalStateException("invalid viewType")
}
}
}
Jordan Terrell
02/07/2020, 8:51 PMoverride fun onCreateViewHolder(..., viewType: Int): BaseHolder<out PossibleRow>
???Jordan Terrell
02/07/2020, 8:52 PMBaseHolder<out PossibleRow>
?kyleg
02/07/2020, 11:18 PMoverride fun onBindViewHolder(holder: BaseHolder<out PossibleRow>, position: Int) {
holder.bind(sets[position])
}
kyleg
02/07/2020, 11:19 PMout
. I had to add the out
in the parameter signature. Then I had to update the function to be:
override fun onBindViewHolder(holder: BaseHolder<out PossibleRow>, position: Int) {
when(holder) {
is BaseHolder.AHolder -> holder.bind(sets[position] as PossibleRow.A)
is BaseHolder.BHolder -> holder.bind(sets[position] as PossibleRow.B)
}
}
kyleg
02/07/2020, 11:19 PMout
is needed, though. 😕