Android RecyclerView with Kotlin – Part 3

In the previous tutorial, we created our data class for a User. We can now use that Model to create a lot of users. In this example, we use a for loop to create 101 users. (Remember, counting starts at zero.) This Int can be any number, though. Let’s create our sample data.

If you run the app, you will see that the RecyclerView works! You can scroll up and down the list of users. If you scroll up and down, you will notice that there is a large gap between each row.

The large gap between the top and bottom rows. (Scroll the emulator to see in action.)

To fix that, we need to open row_user.xml and set the parent’s layout_height to wrap_content.

Set the layout_height to wrap_content.

Run the app again and you should see a long list of Welcome Daniel Malone!

Let’s remove the “Welcome, Daniel Malone” text and show only the first name. Modify the row_user.xml file to add an id of firstName. Also, we change the android:text to tools:text, then import the tools by clicking on tools and pressing Alt+Enter on Windows (or Option+Enter on Mac). With these two modifications, we have the following file.

With our android:id set, let’s create a connection between the row_user.xml layout file and the UsersAdapter class. With UsersAdapter.kt open, make the following modifications.

Rename the onBindViewHolder parameters to something more meaningful. Also, create a link between the firstName android:id and the UsersAdapter.kt file.

Inside of onBindViewHolder, add the following line of code. This line gets the users variable passed in from MainActivity.

Set the firstName based upon the user’s position variable. Use the firstName method on the User object to display the firstName according to the MainActivity class.

Run the app again, and we will see “Daniel” written on the screen 101 times.

The Android Emulator.

If we want to confirm that each row is a individual, unique record, open MainActivity and add the i variable to the firstName by using $i.

Use the i variable for testing.
The Android Emulator.

We now have a working RecyclerView! In the next part of this series, we’ll style each row.

About the author

Daniel Malone
Bringing more than a decade of software engineering experience, Daniel Malone is Editor at androidEveryday. An Austin native, Daniel is often found reading technical books, blogging and creating YouTube tutorials. When not working, he likes to listen to pop hits on Google Play Music.

findViewById() in Kotlin

  • As you may have discovered, findViewByid() is no longer needed.
  • Kotlin adds the ability to directly access Views.

1 week ago

Android Architecture Components Tutorial: ViewModel + LiveData

  • Use Kotlin to build a basic Android app using Android Architecture Components.
  • As part of Jetpack, LiveData and ViewModel support a MVVM app architecture.

3 weeks ago

Android SharedPreferences Tutorial in Kotlin

  • Use SharedPreferences to store simple data in Android Studio projects.
  • It's good for storing small amounts of data

    3 weeks ago