From the course: Building an Android App with Jetpack Libraries

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Add sample notes to the database

Add sample notes to the database - Android Tutorial

From the course: Building an Android App with Jetpack Libraries

Start my 1-month free trial

Add sample notes to the database

- [Instructor] I'm now ready to add real data to my database. All of my app's database work will be done in my ViewModel class. And I'll open the main ViewModel class first. In order to get a reference to my database, I'll need a context. And right now my ViewModel is derived from the ViewModel class and that doesn't provide the information I need. So I'm going to change from the ViewModel as the superclass, to Android ViewModel. In order to initialize this, I'll need a reference to my application and I'll get that first by changing the class constructor for my class, I'll get a parameter named App and it'll be an instance of the application class. And then I'll pass that to the AndroidViewModel Superclass. Now I have the context I need. Now I'll get a reference to my database. I'll start off with private val I'll create a variable named database and I'll initialize it with AppDatabase.getInstance. And I'll…

Contents