Practice: Build Bus Schedule app

1. Before you begin

Introduction

In the Persist Data with Room codelab, you learned how to implement a Room database in an Android app. This exercise provides the opportunity to gain more familiarity with the implementation of Room databases through an independently driven set of steps.

In this practice set, you take the concepts you learned from the Persist Data with Room codelab to complete the Bus Schedule app. This app presents the user with a list of bus stops and scheduled departures using data provided from a Room database.

The solution code is available at the end. To make the most of this learning experience, try to implement and troubleshoot as much as you can before you look at the provided solution code. It is during this hands-on time that you learn the most.

Prerequisites

What you'll need

  • A computer with internet access and Android Studio
  • The Bus Schedule starter code

What you'll build

In this practice set, you complete the Bus Schedule app by implementing a database and then delivering data to the UI using the database. A database file in the asset directory found in the starter code provides data for the app. You load this data into a database and make it available for read usage by the app.

After you complete the app, it shows a list of bus stops and corresponding arrival times. You can click an item in the list to trigger navigation to a detail screen that provides data for that stop.

The completed app shows this data, loaded from a Room database:

7fbafebbcc3a3eec.png

951621eedfc138fb.png

2. Download the starter code

  1. In Android Studio, open the basic-android-kotlin-compose-training-bus-schedule folder.
  2. Open the Bus Schedule app code in Android Studio.
  3. Click the Run button 3e12ddc048503bc5.png to build and run the app.

The app is expected to display a schedule showing one stop when built from the starter branch code.

54aa202086b74e1c.png

3. Add dependencies

Add the following dependencies to the app:

app/build.gradle.kts

implementation("androidx.room:room-ktx:${rootProject.extra["room_version"]}")
implementation("androidx.room:room-runtime:${rootProject.extra["room_version"]}")
ksp("androidx.room:room-compiler:${rootProject.extra["room_version"]}")

You should get the most current stable version of room from the Room documentation and add the correct version number. At this moment the latest version is:

build.gradle.kts

set("room_version", "2.5.1")

4. Create a Room entity

Convert the current Bus Schedule data class into a Room Entity.

The following image shows a sample of what the final data table looks like, including the schema and Entity property.

d2ac3220c9a0f3ff.png

5. Create a data access object

Create a data access object (DAO) to access the database. The DAO provides a method to retrieve all the items in the database and a method to retrieve a single item with the name of the bus stop. Make sure to order the schedule by arrival time.

6. Create a database instance

Create a Room database that uses the Entity and your DAO. The database initializes itself with data from the assets/database/bus_schedule.db file in the starter code.

7. Update the ViewModel

Update the ViewModel to retrieve data from the DAO and provide it to the UI instead of supplying sample data. Make sure to leverage both of your DAO methods to supply data for the list and for individual stops.

8. Solution code