Having a RecyclerView isn't enough. You also need an adapter. Learn about adapters in this video.
This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4534-your-second-kotlin-android-app/lessons/4
Having a RecyclerView isn't enough. You also need an adapter. Learn about adapters in this video.
Hi @bdmoakley !
It might be my Android version but I saw that on the MainActivity
there are two variables: fab
and toolbar
that are not declared and assigned through findViewById
but auto-referenced with the import:
import kotlinx.android.synthetic.main.activity_main.*
So the question is:
Which way is more efficient than the other?
Hi David and Brian!
Those views are referenced using Kotlin Android Extensions. You can read more about KAE here: https://www.raywenderlich.com/84-kotlin-android-extensions
Kotlin Android Extensions utilize a cache and so it’s possible they can be more efficient than using findViewByID()
. The best part is you need less code to reference your views, so they are definitely more efficient from a developer-perspective.
Hi, I use latest Android Studio, and when type
class ListSelectionViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView)
has error : type mismatch, required View
found View?
, then I change it to just View
. Is there any explaination for this since in the view no problem.
@bdmoakley @macsimus Can you please help with this when you get a chance? Thank you - much appreciated! :]
Thanks for the question @telornaga!
As the Android SDK and support libraries evolve, Google has been adding nullability annotations into the Java code. So it will sometimes happen that a constructor, like that here for RecyclerView.ViewHolder, goes from taking a nullable parameter to needing a non-nullable parameter. That seems to be what happened here. You’ll also see this sometimes when you use Ctrl+I to add shells for overrides for an interface you’re implementing, and need to make the parameters non-nullable.
I hope that helps. Thanks again!
@bdmoakley this causing App crash because is not null safe i think so , so what is the solution of it.
Am using android studio 3.5.3.
And even the solution from @macsimus in the below is not working
just removed TODO() from the method
indent preformatted text by 4 spaces override fun onBindViewHolder(holder: ListSelectionViewHolder, position: Int) {
}
What’s the complete code for your method?
It was empty but now it has the below
indent preformatted text by 4 spaces if (holder != null)
{
holder.listpostion.text = (position + 1).toString()
holder.listTitle.text = listTitles[position] //since it will pass every index so the array will read it one by one
}
I will go ahead and change the nullable View? to a non-nullable View like @telornaga suggested and see what happens later. Crossing my fingers that I don’t get an app crash later like what happened to @bdmoakley. I am also on Android Studio 3.5.3.
This course is out of date - it probably causes some minor issues with Android Studio 3.5 - you can find our latest version over here: https://www.raywenderlich.com/7503473-your-second-kotlin-android-app