I have two fragment 1. Test Fragment 2. Test Detail Fragment
1. Test Fragment I would like to display
- Test 1 - 15/9/23 9:00(current date and time)
- Test 2 - 15/9/23 9:15(current date and time)
2. Test Detail Fragment I wiould like to display
- Clicking on item one from the List view it will look like this
- Test1 (name of list view item)
- 1. abc2 - 12s,
- 2. abc2 -10s,
- 3. abc3 - 30s
In my kotlin app with room database entity Test and TestDetails. How do i create dao and view model and get that value into fragment?

Tuhin PaulRank #7 · 71.9K pointsPosted Apr 25, 2023, 2:12 AM EDT
Part 1:
To retrieve the data from the Room database and display it in the appropriate fragments, Define the entities for the "Test" and "Test Detail" tables in the Room database.
@Entity(tableName = "test") data class Test( @PrimaryKey val id: Int, val name: String, val startTime: Long ) @Entity(tableName = "test_detail") data class TestDetail( @PrimaryKey val id: Int, val testId: Int, val detail: String, val duration: Int )Tuhin PaulRank #7 · 71.9K pointsPosted Apr 25, 2023, 2:13 AM EDT
Part 2:
Also Create a data access object (DAO) for each entity to define the methods for accessing the data.
@Dao interface TestDao { @Query("SELECT * FROM test") fun getAllTests(): LiveData> @Query("SELECT * FROM test WHERE id = :testId") fun getTestById(testId: Int): LiveData } @Dao interface TestDetailDao { @Query("SELECT * FROM test_detail WHERE testId = :testId") fun getTestDetailsForTest(testId: Int): LiveData> }And then Create a database class that extends the RoomDatabase class and defines the methods for accessing the DAOs.
@Database(entities = [Test::class, TestDetail::class], version = 1) abstract class AppDatabase : RoomDatabase() { abstract fun testDao(): TestDao abstract fun testDetailDao(): TestDetailDao }