Beginning RecyclerView - Part 4: Binding the | Ray Wenderlich Videos

Learn how to connect the model data displayed in the RecyclerView to the corresponding objects in the view layer.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4568-beginning-recyclerview/lessons/4

I have a question.

the class Viewholder is as follow:

class ViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {

private fun bindView(creature: Creature) {

}
}

I just used autocomplete and it gave me the above class definition. with itemView with a "?.
I know what that means, that the itemView can be null.
My Question is

is it always better to handle such nullable possibilities like this

class ViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {

private fun bindView(creature: Creature) {
itemView.let {
it.creatureNameTextView.text = creature.fullName
it.creatureImageView.setImageResource(itemView.context.resources.getIdentifier(creature.uri, null, itemView.context.packageName))
}
}
}

using the let function?

Also I know that it is not necessary to have “?” in the constructor of ViewHolder but i just wanted to clear the unwrapping.

I hope this makes sense.

Thank you

@macsimus Can you please help with this when you get a chance? Thank you - much appreciated! :]

Hi @sagarl, thanks for the question!

The RecyclerView library is built and Java, and the autocomplete apparently is using the fact that the Java code uses the @Nullable annotation on itemView.

You can either use let or a null-check via if to handle the nullable.

I hope that helps! Thanks again!