Reader small image

You're reading from  Android Programming with Kotlin for Beginners

Product typeBook
Published inApr 2019
Reading LevelIntermediate
PublisherPackt
ISBN-139781789615401
Edition1st Edition
Languages
Right arrow
Author (1)
John Horton
John Horton
author image
John Horton

John Horton is a programming and gaming enthusiast based in the UK. He has a passion for writing apps, games, books, and blog articles. He is the founder of Game Code School.
Read more about John Horton

Right arrow

Chapter 16. Adapters and Recyclers

We will achieve much in this brief chapter. We will first go through the theory of adapters and lists. We will then look at how we can use a RecyclerAdapter instance in Kotlin code and add a RecyclerView widget to the layout, which acts as a list for our UI, and then, through the apparent magic of the Android API, bind them together so that the RecyclerView instance displays the contents of the RecyclerAdapter instance and allows the user to scroll through the contents of an ArrayList instance full of Note instances. You have probably guessed that we will be using this technique to display our list of notes in the Note to self app.

In this chapter, we will do the following:

  • Explore another type of Kotlin class – the inner class

  • Look at the theory of adapters and examine binding them to our UI

  • Implement the layout with RecyclerView

  • Lay out a list item for use in RecyclerView

  • Implement the adapter with RecyclerAdapter

  • Bind the adapter to RecyclerView

  • Store notes...

Inner classes


In this project, we will use a type of class we have not seen yet – an inner class. Suppose that we have a regular class called SomeRegularClass, with a property called someRegularProperty, and a function called someRegularFunction, as shown in this next code:

class SomeRegularClass{
    var someRegularProperty = 1    

    fun someRegularFunction(){
    }
}

An inner class is a class that is declared inside of a regular class, like in this next highlighted code:

class SomeRegularClass{
    var someRegularProperty = 1

    fun someRegularFunction(){
    }

    inner class MyInnerClass {
       val myInnerProperty = 1

       fun myInnerFunction() {
       }
    }

}

The preceding highlighted code shows an inner class called MyInnerClass, with a property called myInnerProperty, and a function called myInnerFunction.

One advantage is that the outer class can use the properties and functions of the inner class by declaring an instance of it, as shown highlighted in the next code snippet...

RecyclerView and RecyclerAdapter


In Chapter 5, Beautiful Layouts with CardView and ScrollView, we used a ScrollView widget and we populated it with a few CardView widgets so that we could see it scrolling. We could take what we have just learned about ArrayList and create a container of TextView objects, use them to populate a ScrollView widget, and, within each TextView, place the title of a note. This sounds like a perfect solution for showing each note so that it is clickable in the Note to self app.

We could create the TextView objects dynamically in Kotlin code, set their text property to be the title of a note, and then add the TextView objects to a LinearLayout contained in ScrollView. But this is imperfect.

The problem with displaying lots of widgets

This might seem fine, but what if there were dozens, hundreds, or even thousands of notes? We couldn't have thousands of TextView objects in memory because the Android device might simply run out of memory, or, at the very least, grind...

Adding RecyclerView, RecyclerAdapter, and ArrayList to the Note to Self project


Open the Note to self project. As a reminder, if you want to see the completed code and working app based on completing this chapter, it can be found in the Chapter16/Note to self folder.

Note

As the required action in this chapter jumps around between different files, classes, and functions, I encourage you to follow along with the files from the download bundle open in your preferred text editor for reference.

Removing the temporary "Show Note" button and adding RecyclerView

These next few steps will get rid of the temporary code we added in Chapter 14, Android Dialog Windows, and set up our RecyclerView ready for binding to RecyclerAdapter later in the chapter:

  1. In the content_main.xml file, remove the temporary Button with an id of button, which we added previously for testing purposes.

  2. In the onCreate function of MainActivity.kt, delete the Button instance declaration and initialization along with the lambda that...

Running the app


You can now run the app and enter a new note, as shown in this next screenshot:

After you have entered several notes of several types, the list (RecyclerView) will look something like this next screenshot:

And, if you click to view one of the notes, it will look like this:

Note

Reader challenge

We could have spent more time formatting the layouts of our two dialog windows. Why not refer to Chapter 5, Beautiful Layouts with CardView and ScrollView, as well as the Material Design website, https://material.io/design/, and do a better job than this. Furthermore, you could enhance the RecyclerView list of notes by using CardView instead of LinearLayou t.

Don't spend too long adding new notes, however, because there is a slight problem: close and restart the app. Uh oh, all the notes are gone!

Frequently asked questions


Q.1) I still don't understand how RecyclerAdapter works?

A) That's because we haven't really discussed it. The reason we have not discussed the behind-the-scenes details is because we don't need to know them. If we override the required functions, as we have just seen, everything will work. This is how RecyclerAdapter and most other classes we use are meant to be: hidden implementation with public functions to expose the necessary functionality.

Q.2) I feel like I need to know what is going on inside RecyclerAdapter and other classes as well. How can I do this?

A) It is true that there are more details for RecyclerAdapter (and almost every class that we use in this book) that we don't have the space to discuss. It is good practice to read the official documentation of the classes you use. You can read more about it at https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.

Summary


Now we have added the functionality to hold multiple notes and implemented the ability to display them.

We achieved this by learning about and using the RecyclerAdapter class, which implements the Adapter interface, which allows us to bind together a RecyclerView instance and an ArrayList instance, allowing for the seamless display of data without us (the programmer) having to worry about the complex code that is part of these classes, and which we don't even see.

In the next chapter, we will start with making the user's notes persist when they quit the app or switch off their device. In addition, we will create a "Settings" screen, and see how we can make the settings persist as well. We will use different techniques to achieve each of these goals.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Android Programming with Kotlin for Beginners
Published in: Apr 2019Publisher: PacktISBN-13: 9781789615401
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at AU $19.99/month. Cancel anytime

Author (1)

author image
John Horton

John Horton is a programming and gaming enthusiast based in the UK. He has a passion for writing apps, games, books, and blog articles. He is the founder of Game Code School.
Read more about John Horton