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
users. If you scroll up and down, you will notice that there is a large gap between each row.
To fix that, we need to open
row_user.xml and set the parent’s
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
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.
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.
onBindViewHolder, add the following line of code. This line gets the
users variable passed in from
Run the app again, and we will see “Daniel” written on the screen 101 times.
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
We now have a working
RecyclerView! In the next part of this series, we’ll style each row.