How will you fix that code?
Dmitri Kudrenko
11

Its a good question, will add sth about it.

Depends a bit on the route you went. 
I would probably build a base ViewHolder that uses generics to be type safe. The base is needed to avoid the cast:

abstract class BetterviewHolder<T>(parent: ViewGroup) : RecyclerView.ViewHolder(parent) {
abstract fun bind(item: T)
}

where T is either the model class or the ViewModel depending on the approach.

And then the implementation would look like:

override fun onBindViewHolder(holder: BetterviewHolder<Visitable>, position: Int)
= holder.bind(things.get(position))

or similar for the ViewModel:

override fun onBindViewHolder(holder: BetterviewHolder<ViewModel>, position: Int)
= holder.bind(things.get(position))
Like what you read? Give Danny Preussler a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.