Search code examples
javaandroidviewmodelandroid-roomandroid-livedata

How to use Android Room to store data records


I want to store team information in an Android Room database. The information contains team name, players, batting, fielding, and bowling statistics.

These are my requirements:

  • Team Names have to display in first recyclerview as a list
  • When a user selects a team, the user will navigate to a team information screen that shows all players in the team
  • If a user selects a player, the user will navigate to the player info screen that shows the player's information like bowling, batting, and Fielding statistics

Can anyone please give me suggestions to fulfill these requirements?


Solution

  • I can suggest you to store team names & players into separate tables with its primary and foreign keys, respectively, in order to make relation between them. After all, if you need to fetch stored data, you can join tables, or get by team name (if it is primary key) or anything else.

    You can read more about @Relation here: https://developer.android.com/reference/android/arch/persistence/room/Relation

    Relation can help you handle one-to-many, many-to-many states.

    Note: do all tasks in background thread with the help of Rx/Coroutines.