Skip to content
Advertisement

Can I Entity Room data right from RecyclerView and Save it to Room Database?

I have some adapter which use retrofit to get data right from web api and place it to recyclerview

JavaScript

This is my recyclerview This works only when network is enabled

The question is how to set this data right from RecyclerView (or other way) to my Room DataBase when network is enabled by Onclick SaveButton to create other recyclerview and set data there, to get it offline later.

I’m trying to create Entity

JavaScript

Dao

JavaScript

and DataBase

JavaScript

Which way do I need to create it?

Advertisement

Answer

  1. Create an @Entity Notice which is your data type to be stored in your Room DB.
  2. Create a View Model which is attached to your Activity/Fragment where you need to show this list.
  3. Use your ViewModel to store the list from API into your Room DB.
  4. Create a LiveData which observes on the DB and sends the updated list to the other view.

Code for Saving Data in DB. This needs to be run on Background Thread.

JavaScript

ViewModel

JavaScript

Activity Code where you need to show the saved data

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement