In part one of this series, we set up the basic RecyclerView
structure. In this the second part, we will finish creating the adapter. Let’s begin!
First, make ViewHolder
extend the correct class, RecyclerView.ViewHolder
, and pass in the view from the ViewHolder
class. Your code should now look like this:
If you look closely, you will see the Android Studio is throwing an error under class UsersAdapter
, as indicated by a red line under class UsersAdapter. Let’s fix that!

Click on UsersAdapter, then, on your keyboard, press Alt+Enter on Windows (or Option+Enter on a Mac). Then click implement members. Now, select all three methods, and press OK.

Android Studio will now stub out the following methods, and should look like the following.

Next, let’s focus on the data that we will insert into this RecyclerView
. Let’s have UsersAdapter
receive a users variable, of type List<User>
. This indicates that we will have a List
of User
objects. There could be 10 users, 50 users, or 423 users. At this point, we really don’t care. We just know that UsersAdapter
will receive a List
of Users
.
Next, let’s turn our attention to the getItemCount
method. This method needs to return the total number of items in this RecyclerView
. In the last step, we made UsersAdapter
receive a List
of Users
. Now, we can use the users variable to get the size of the List.

Next, let’s work on onCreateViewHolder
. This method is responsible for setting the XML layout file we want to use. In the part one, we created row_user.xml
. We’ll use that layout here.
First, let’s rename the variables of the method to give them more meaning. p0 and p1 are not great variable names. Let’s change that to parent and viewType
, as you can see above. Next, we create a variable named view and use LayoutInflater.from to create — or inflate — the XML layout file. Then we return ViewHolder
, passing in our view variable.
Change the return type of onCreateViewHolder
to ViewHolder
, the class at the bottom of the UsersAdapter.kt
file.

After setting up onCreateViewHolder
, let’s focus on how the RecyclerView
receives the List
of Users. Open content_main.xml and ensure that the RecyclerView
tag has an android:id
attribute set to recyclerView
.

With content_main.xml finished, open MainActivity.kt and delete the boilerplate code to make the file look like the following.
We can now setup the RecyclerView in MainActivity. Using a new Kotlin feature, let’s code the following.

You’ll notice the red variable users on line 18 above. The only task left to do is to pass the users variable from MainActivity
into UsersAdapter
. For now, let’s fill the RecyclerView
with generated content. To start, we need to create a Model that holds our data.

Set the class Name of User and Kind of Class. This will automatically generate a User.kt file for us that we can use to store the user’s info.
Let’s convert this class into a data class
and add a firstName
and lastName
, both as a String
.

In part three, we’ll finish coding the RecyclerView!