Search code examples
javaandroidandroid-recyclerviewlayout

How to store a layout to SQlite database in order to populate it later in the Recyclerview? Like the image shown below


What is the idea to store a dynamic checkboxes list into SQLite database. I know how to add checkboxes with text into the Edittext and later add the view to the layout, after that I would be having a layout like the image below, but the question is, how to store that layout to SQlite table in order to be able to show that layout in the Recyclerview row when clicked.enter image description here


Solution

  • Just make your data class (something like) Note stored an extra var, checked like so:

    data class Note(
        val note: String,
        var checked: Boolean = false
    )
    

    Then you can just save a list of those to the SQLite DB List<Note>

    @Dao
    interface NoteDao {
    
        @Query("SELECT * FROM noteTable")
        suspend fun getAllNotes(): List<Note>
    
        @Insert
        suspend fun insertNote(note: Note)
    
        @Delete
        suspend fun deleteNote(note: Note)
    
        @Query("DELETE FROM noteTable")
        suspend fun deleteAllNotes()
    
    }
    

    And then when you get (load the screen) the data you can set the checkbox based on the value of checked.

    You can read more about setting up your ROOM, DAO and SQLite database here. This tutorial will help you get setup with the different layers, here a reminder/note has only a String, but you can easily add more values to the data class.