1

There is an event in my project and there are timelines in this event. Each event has its own timelines. I want to list the timelines of each event with the codes I have written, but I cannot access it via the adapter.

My entities;

Event:

@Parcelize
@Entity(tableName = "event_table")
data class Event(
    @PrimaryKey(autoGenerate = true)
    val eventId: Int,
    val eventName: String,
    val eventCategory: String
): Parcelable

Timeline:

   @Parcelize
    @Entity(tableName = "timeline_table")
    data class Timeline(
        @PrimaryKey(autoGenerate = true)
        val TimeLineId: Int,
        val TimeLineAccomplish: String,
        val TimeLineDetails: String,
        val TimeLineDate: String,
        val eventCreatorId: Int
    ): Parcelable

EventAndTimeline:

data class EventAndTimeline(
    @Embedded val event: Event,
    @Relation(
        parentColumn = "eventId",
        entityColumn = "eventCreatorId"
    )
    val timeline: List<Timeline>
)

Database:

@Database(entities = [Timeline::class, Event::class], version = 1, exportSchema = false)
abstract class TimeLineDatabase: RoomDatabase() {
    abstract fun timelineDao(): TimelineDao
    abstract fun eventDao(): EventDao

    companion object{
        @Volatile
        private var INSTANCE: TimeLineDatabase? = null

        fun getDatabase(context: Context): TimeLineDatabase {
            val tempInstance = INSTANCE
            if (tempInstance != null) {
                return tempInstance
            }
            synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    TimeLineDatabase::class.java,
                    "timeline_database"
                )
                    .build()

                INSTANCE = instance
                return instance
            }
        }
    }
}

Dao:

@Dao
interface TimelineDao{
    @Transaction
    @Query("SELECT * FROM event_table")
    fun getEventsWithTimelines(): LiveData<List<EventAndTimeline>>
}

Repository:

class TimelineRepository(private val timelineDao: TimelineDao) {
   
    val readAllData: LiveData<List<EventAndTimeline>> = timelineDao.getEventsWithTimelines()
}

ViewModel:

class TimeLineViewModel(application: Application): AndroidViewModel(application) {

    val readAllData: LiveData<List<EventAndTimeline>>
  
    private val timelineRepository: TimelineRepository

    init {
        val timelineDao = TimeLineDatabase.getDatabase(application).timelineDao()
        timelineRepository = TimelineRepository(timelineDao)
        readAllData = timelineRepository.readAllData
    }

Finally: In my List adapter i want to list timelines of event. When i didnt have a relation i reach all timelines with:

holder.itemView.findViewById<TextView>(R.id.timelineDetails_txt).text 
currentItem.TimeLineDetails 

but now I can't see timelines variables(Like TimeLineDetails, TimeLineAccomplish etc.).

1 Answers1

0

You need to write a query like the below example.

  interface UserBookDao {
        @Query(
            "SELECT user.name AS userName, book.name AS bookName " +
            "FROM user, book " +
            "WHERE user.id = book.user_id"
        )
        fun loadUserAndBookNames(): LiveData<List<UserBook>>
    
        // You can also define this class in a separate file.
        data class UserBook(val userName: String?, val bookName: String?)
    }

you can check more examples in below link.

https://developer.android.com/training/data-storage/room/relationships

Jai Khambhayta
  • 4,198
  • 2
  • 22
  • 29
  • 1
    Thanks a lot for your help but still I need clarification. Yes, this time I reached the variables, but all the elements are listed, not the elements of the event I clicked on. I am using a recyclerview. For example, when clicking on the first event, will list the elements of that event. Here, after the user clicks on a first element, do I need to get the id of that element and make a comparison on the adapter? –  Apr 11 '22 at 12:29
  • nop you have to check on the database. e.g event object with eventId1 should return the list which have same eventCreatorId. if the response is not correct check your query and database too. – Jai Khambhayta Apr 11 '22 at 12:46
  • Still, I couldn't handle it but I will read the documents that you sent. Maybe I can understand better. –  Apr 11 '22 at 12:59
  • cool !! just first try to get the correct response from the database. make sure about the response print the response and then you can move to the list. – Jai Khambhayta Apr 11 '22 at 13:03