‫Welcome back.
‫Let's continue with the view layer of the Mvvm architecture.
‫And in this video, we're going to display all the data, all the contacts received from the room database
‫into the main activity recycler view.
‫For that, I need to define data, sources, adapters and binding objects.
‫Starting by data source.
‫I need to make a contact database object named as contact database and I'll create an array list of
‫contacts objects.
‫Here I need to create an object of adapter class private my adapter and here I need to create objects
‫for data binding.
‫I have two variables one for the activity main binding, main binding and one for the click handler
‫which we created before main activity Click handlers.
‫And this is the class that we created before that handles the click events on the Fab Floating Action
‫button.
‫Let's initialize them here in the Oncreate method.
‫Main binding equal to data binding util dot Setcontentview this and passing our dot layout dot activity
‫main data binding util dot Setcontentview is a method provided by Android's data binding library that
‫allows you to set the content view of an activity or a fragment with data binding enabled.
‫It simplifies the process of using data binding to bind UI elements to data sources in your layout XML
‫files.
‫Then I need to initialize the handlers equal to new main activity handlers and I'll pass this as a context.
‫Then I need to link the main activity binding dot set click handler to the handlers.
‫So here I am linking this main activity click handlers to the main activity binding.
‫Let's move to initialize our recycler view and define the layout manager for it.
‫Recycler View Recycler View equal to activity Main binding dot recycler View Recycler View Dot Set Layout
‫Manager.
‫New linear layout manager passing this and recycler view dot set has fixed size to true.
‫In this line, I am obtaining a reference to a recycler view widget from a layout that uses data binding
‫in Android.
‫This is called main binding, so this is an object that uses and refers to this activity.
‫Underscore main.
‫And I'm going to use a reference for the recycler view called recycler view here.
‫So this line is used to obtain a reference to a recycler view widget from a layout that uses data binding
‫in Android.
‫This reference allows you to interact with and manipulate the recycler view programmatically in your
‫Android application code using the data binding library.
‫Then let's move to create the adapter equal to new my adapter and I'll pass contacts as an array list.
‫Let's move to the database contact database equal to contact database dot get instance and I need to
‫pass the context get instance.
‫As I told you before in the contact database, it will return one instance of the contact database because
‫we followed the singleton pattern.
‫Then I need to use the view model.
‫So I'll create my ViewModel instance.
‫ViewModel New ViewModel provider this dot get my view model dot class.
‫So this line of code is used to get and create and retrieve an instance of a ViewModel associated with
‫a specific activity or fragment.
‫Let's break it down.
‫ViewModel Provider is a class provided by Android's ViewModel architecture component.
‫It's used to create and manage view models within the scope of an activity or fragment.
‫This keyword typically refers to the current activity or fragment instance in the context of your code.
‫This is passed as an argument to the view model provider.
‫It signifies that you want to create or retrieve a ViewModel associated with the current activity or
‫fragment.
‫The get method of ViewModel Provider is called with a class of the ViewModel you want.
‫This method is responsible for creating a new instance of the ViewModel if it doesn't already exist
‫or returning an existing instance.
‫If it does, then I'll insert a new contact just for testing.
‫Later on we'll create a new activity that it's.
‫Responsible for creating and inserting new contacts into the database.
‫I'll start with the contacts.
‫New contact.
‫The ID would be one.
‫This not important because the database.
‫The database will auto generate the primary key.
‫Then the name would be Jack and the email is Jack at gmail.com.
‫I need to insert it into our database, so I'll use view model dot add new contact.
‫You can see I am dealing with the view model.
‫I don't deal directly with the database, I deal with the view model and the view model will deal with
‫the repository and the repository will deal with the room database.
‫This is how the Mvvm architecture works.
‫Then I need to use the recycler view dot set adapter linking the adapter with the recycler view adapter
‫and before linking the recycler view, I need to load the data from the database.
‫What I need to do is.
‫View model dot.
‫Get all contacts.
‫Then I need to use the observe method and the observe method is used to observe or listen for changes
‫in the underlying data held by the live data object.
‫You use the observe method on a live data object and pass in an observer you created.
‫The Observe method establishes a connection between the live data and the observer.
‫It tells the live data to notify the observer when the data changed.
‫I need to pass two parameters one for the owner and one for the observer.
‫So I am linking here the Observer with the observe, and I'm going to override the on changed method,
‫and the observer object defines the behavior to execute when the user live data changes.
‫In this case, it's reacting to changes in the context data.
‫I'll use a for loop for each loop it's of type context.
‫See inside the contacts array list.
‫At first I'll get them into the logcat and then display them into the recycler view.
‫So for that I'll make log dot v tag and c dot get name.
‫I'll display all the names of the contacts in my database.
‫Let's run this application and test our code.