How Do You Retreive And Update An Item Using Room Database In Jetpack Compose?

I’m trying to develop a simple note taking app in jetpack compose. I’ve successfully developed an application that takes in text field inputs and saves them using room database then displays them into a lazy column.

I’ve create the basic room files. Here they are:

The Note Entity takes one parameter (other than the id) called text.

@Entity
data class Note(

    val Text: String,

    @PrimaryKey(autoGenerate = true)
    val id: Int = 0
)

The Doa has five functions. One for inserting a note, one for updating a note, one for deleting a note, one for retrieving all notes, and one for retrieving a note by it’s id.

@Dao
interface NoteDao {

    @Insert
    suspend fun insertNote(note: Note)

    @Update
    suspend fun updateNote(note: Note)

    @Delete
    suspend fun deleteNote(note: Note)

    @Query("SELECT * FROM note")
    fun getAllNotes(): Flow<List<Note>>

    @Query("SELECT * FROM note WHERE id = :id")
    fun getNoteById(id: Int): Flow<Note>
}

Here is the database as you’d expect.

@Database(
    entities = [Note::class],
    version = 1
)
abstract class NoteDatabase: RoomDatabase() {
    abstract val noteDao: NoteDao
}

Here is the view model. It takes all of the Dao functions.

class NoteViewModel(private val noteDatabase: NoteDatabase): ViewModel() {

    fun insertNote(note: Note){
        viewModelScope.launch {
            noteDatabase.noteDao.insertNote(note)
        }
    }

    fun updateNote(note: Note){
        viewModelScope.launch {
            noteDatabase.noteDao.updateNote(note)
        }
    }

    fun deleteNote(note: Note){
        viewModelScope.launch {
            noteDatabase.noteDao.deleteNote(note)
        }
    }

    fun getAllNotes() = noteDatabase.noteDao.getAllNotes()
        .asLiveData(viewModelScope.coroutineContext)

    fun getNoteById(id: Int) = noteDatabase.noteDao.getNoteById(id)
        .asLiveData(viewModelScope.coroutineContext)

}

Here is the Main Page Composable. It contains a column that displays two text fields, one holds a variable called “textValue” and the other holds a variable called “updateValue”. Beneath the text fields are two buttons. One successfully saves the value of the first text field and displays it in a lazy column, the other unsuccessfully updates the value of a saved note. Beneath buttons is a lazy column that displays the saved notes.

@Composable
fun MainPage(viewModel: NoteViewModel) {
    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.spacedBy(20.dp),
        modifier = Modifier
            .fillMaxSize()
            .padding(50.dp)
    ) {

        val note = Note(
            Text = textValue
        )

        var noteList by remember { mutableStateOf(listOf<Note>())}

        viewModel.getAllNotes().observe(LocalLifecycleOwner.current) {
            noteList = it
        }

        NoteTextField(text = textValue, onTextChange = { textValue = it })

        NoteTextField(text = updateValue, onTextChange = { updateValue = it })

        Row {
            NoteButton(
                text = "Create",
                onClick = {
                    viewModel.insertNote(note)
                }

            )
            NoteButton(
                text = "Update",
                onClick = {
                        viewModel.updateNote(
                        note = Note(updateValue))
                }
            )
        }
        NoteList(noteList = noteList)
    }
}

Here the NoteList composable. It contains a lazy column, that holds a box that displays the saved note. When a box is pressed the saved note is displayed in the second text field.

@Composable
fun NoteList(
    noteList: List<Note>
    ) {
    LazyColumn {
        items(noteList){note ->
        Box(
            contentAlignment = Alignment.Center,
            modifier = Modifier
                .clickable {
                    savedText = note.Text
                    updateValue = note.Text
                }
                .border(width = 1.dp, color = Color.Black)
                .fillMaxWidth()
                .height(100.dp)
        ){
            Text(text = "${note.Text}    ${note.id}")
        }
    }}
}

I can’t figure out how to update the saved note after it is displayed in the second text field.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật