Skip to content
Share
Explore

Lab 1: November 20 2025: Books and Recycler Views

Learning outcomes:
RecyclerView
Adapter
List of custom objects
Navigation to an Edit screen
Updating data
Shared data storage (in memory only)

Recycler view and edit screen: Managing a list of Books.

Book.kt
BookRepository.kt
item_book.xml
activity_main.xml
BookAdapter.kt
MainActivity.kt
activity_edit_book.xml
EditBookActivity.kt

Step 1: Book.kt
package com.example.bookapp

data class Book(
var title: String,
var author: String
)

STEP 2 — Create the Shared Repository

⭐ This removes all complexity.

BookRepository.kt
package com.example.bookapp

object BookRepository {
val books = mutableListOf<Book>()
}

This is our in-memory database.

STEP 3 — Create the Item Layout

image.png
res/layout/item_book.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/textTitle"
android:textSize="18sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<TextView
android:id="@+id/textAuthor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

STEP 4 — Create the Main Screen Layout

res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/btnAdd"
android:text="Add Book"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvBooks"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>

STEP 5 — Create the Adapter

BookAdapter.kt BookAdapter brings the recycler view to life. takes a list of books and turns each one into a visible row on the screen.
BookAdapter.kt is the code that tells the RecyclerView how to show each book and how to fill each row with data.
package com.example.bookapp

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class BookAdapter(
// The constructor lets the Activity tell the adapter
// what should happen when a book is clicked.
// Here you are passing into the constructor a Function - A BEHAVIOR
// It returns a Behavior, not a value
// onItemClick — saves the function so the adapter can use it inside the ViewHolder.
// When the user clicks a row, open the EditBookActivity
// and tell it which row was clicked
private val onItemClick: (Int) -> Unit
) : RecyclerView.Adapter<BookAdapter.BookViewHolder>() {

inner class BookViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val title: TextView = view.findViewById(R.id.textTitle)
val author: TextView = view.findViewById(R.id.textAuthor)

init {
view.setOnClickListener {
onItemClick(bindingAdapterPosition)
}
// This means: “When someone taps this row, call the function the Activity gave me,
// and tell it my position.”
}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BookViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item_book, parent, false)
return BookViewHolder(view)
}

override fun onBindViewHolder(holder: BookViewHolder, position: Int) {
val book = BookRepository.books[position]
holder.title.text = book.title
holder.author.text = book.author
}

override fun getItemCount(): Int = BookRepository.books.size
}


🚀 STEP 6 — Create MainActivity

MainActivity.kt
package com.example.bookapp

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

class MainActivity : AppCompatActivity() {

Want to print your doc?
This is not the way.
Try clicking the ··· in the right corner or using a keyboard shortcut (
CtrlP
) instead.