Load Data into RecyclerView from Database
The RankingsViewModel class is similar to GameViewModel in that we’re getting an instance of PennyDropRepository, then getting LiveData back. We use the Transformations class to turn Player and GameStatus objects into PlayerSummary objects. Then, once that’s done, we observe the playerSummaries LiveData value from RankingsViewModel inside RankingsFragment and update PlayerSummaryAdapter with the new data. Let’s go through and get each piece created.
Create RankingsViewModel
As I mentioned, this will look similar to GameViewModel in how we get an instance of PennyDropRepository. The entire RankingsViewModel class will look like this, minus the Transformations.map logic (which we’ll cover soon).
| class ... |
Get Kotlin and Android Development featuring Jetpack now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.