Room I — Entities, DAO, ViewModel, RecyclerView (ToDo app)
Build a persistent ToDo app. Room wraps SQLite. Repository + ViewModel keep DB access off the UI. RecyclerView lists items efficiently.
Key concepts
- @Entity, @PrimaryKey
- @Dao (Insert, Query, Update, Delete)
- @Database (RoomDatabase)
- Repository pattern
- RecyclerView + ListAdapter + DiffUtil
Lectures in this day
- 01
Project Setup
Before writing features, get the project ready. Turn on Data Binding, Navigation, Safe Args, and add Room (the database library) in Gradle. `minSdk` is the oldest Android version your app supports.
Real life · Prepping your kitchen before cooking — knives sharpened, oven on, ingredients ready. Ten minutes now saves an hour later.gradleplugins { id("com.android.application") kotlin("android") kotlin("kapt") // needed for Room id("androidx.navigation.safeargs.kotlin") } dependencies { implementation("androidx.room:room-runtime:2.6.1") implementation("androidx.room:room-ktx:2.6.1") kapt("androidx.room:room-compiler:2.6.1") }Practice · 3 quick questions
Q1.Project Setup means…
Q2.Dependencies are added in…
Q3.A good first check is…
Pick one answer per question. - 02
New ToDo Fragment Page Design
Draw the 'add todo' screen: a title box, a description box, a button to pick date/time, and a Save button. Simple form, one action.
Real life · Like a sticky note on the fridge — but with a date field so 'buy cake' becomes 'buy cake, Saturday 5 pm'.xml<class="tok-key">LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="16dp" android:layout_width="match_parent" android:layout_height="match_parent"> <class="tok-key">EditText android:id="@+id/title" android:hint="Title" android:layout_width="match_parent" android:layout_height="wrap_content" /> <class="tok-key">EditText android:id="@+id/desc" android:hint="Description" android:layout_width="match_parent" android:layout_height="wrap_content" /> <class="tok-key">Button android:id="@+id/pickDate" android:text="Pick date & time" android:layout_width="match_parent" android:layout_height="wrap_content" /> <class="tok-key">Button android:id="@+id/save" android:text="Save" android:layout_width="match_parent" android:layout_height="wrap_content" /> </class="tok-key">LinearLayout>Practice · 3 quick questions
Q1.New ToDo screen mainly needs…
Q2.Text input widget?
Q3.Best container for a form?
Pick one answer per question. - 03
Retrieve Values from Room Group
Read what the user typed into an EditText with `.text.toString()`. Trim spaces if needed. These raw strings become the fields you save into the database.
Real life · Reading answers from a paper form before filing it away — each field goes into your record.Practice · 3 quick questions
Q1.Getting values from EditText uses…
Q2.Empty input should be…
Q3.Trim whitespace with…
Pick one answer per question. - 04
Date/Time Picker Dialog
Show a DatePickerDialog to pick the day, then a TimePickerDialog for the hour. Combine the two into a `Calendar`, then save `timeInMillis` (a single number of milliseconds since 1970). Numbers are easy to compare and sort.
Real life · Setting an alarm: pick the day, then the time. Behind the scenes the phone stores it as one number.kotlinval cal = Calendar.getInstance() DatePickerDialog(requireContext(), { _, y, m, d -> cal.set(y, m, d) 0