Notes App (MVVM + ROOM Database) in Android Studio using Kotlin | Part 3: Adapter & ViewModel

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome back to my channel in this video we will continue with our Notes app in previous video we have cover room database so if you have not yet watched it I'll highly recommend you to watch that first and then watch this video now in this video we will cover view model and adapter so as you know MVM stands for model View and view model out of which we have already created model that is a data view model we will create it today and view we will cover it in the next video then adapter is for Recycler view so first let's start with adapter create a new class under adapter package as node adapter and done now we have only one recycler view present in home fragment to display the notes correct so for that we will create an adapter and a view holder which will basically going to attach the data with its respective UI got it let's start with extending the recycler view do adapter and inside it note adapter dot note view holder which we'll create now so in side note adapter class create another class as note view holder wave holder is basically used for attaching the XML layout file like r. layout do node layout but here we will be using binding so for that here write well item binding as note layout binding that is an XML file extends view holder with item binding do root now whenever we want to access anything from note layout we will write item binding and a list of all the UI components present in it will be displayed next is to set up different call back and Earth in list stiffer now what are those both of them are used together to efficiently update the contents of a recycler view without refreshing the entire data set so let me write the code first and then I'll explain you [Music] and done now see this is a differ call back implementing diff util do item call back interface this interface is used by diff util to determine the difference between two list first function is are items the same is called to check whether the item represents the same object in no data class here it compares the ID no description and no title of the old items with the new items if all these properties are same it considers the item as same similarly second method are contains the same is called to check whether the contents of two items are the same in the node data class here it uses the default equality check that is old item is equal to equal to new item this means that if all the properties of the notes are the same then the contents are considered the same got it then this line creates an Ayn list differ instance using differ call back which we created basically Ayn list differ determine differences between two list on a background threade which helps in smooth UI updates and that's it next we will Implement three members of note adapter class that is on create view holder onbind view holder and get item count first in oncreate view holder we we need to define the view means XML layout file so let me write the code and see as we have used binding so note layout binding will inflate parent do text and returning note view holder obviously next in onbind view holder first Define the position of the note in the list like this way then once we know the position then simply attach the data with its respective UI like this way not title do text through item binding is equal to current note do title means whatever the current node title will be that will be displayed on the Node title text view similarly whatever the current node description will be displayed on the Noe description text view simple now what will happen if I click on the recycler view item means on the note it should navigate the user to edit node fragment correct so first set on click listener on item View and inside it we will set up Nev controller let me write the code and done see Direction variable consist of Home fragment direction that goes from home fragment to edit node fragment of the current node then set the direction on the NV controller and done next is to return size of the list in get item count function so written defer do current list do sites and that's it our note adapter is ready now moving on to our next class that is view model so create a new class under view model package name it as note view model and then now as you know view model act as a bridge between View and model so here we'll be creating five functions that will launch through Co routin scope so for that first we need to add parameters here as application then node repository and extend it as Android view model with argument as app then inside it create a function ad node with Note parameter is equal to view model scope do launch which is a part of the co routine basically it ensures that the co routine is cancelled when the associate view model is cleared or destroyed to prevent potential memory leaks got it then inside the co Rootin block write note repository dot insert note with not argument this will launch in the background using Code routines and done similarly let me quickly create for delete note as well as update Noe and then next we will create get all nodes function is equal to note repository do get all nodes as we are retrieving the data so there is no need of parameters then create another function as search node with query parameter that is equal to search note method from note repository with ARG ment as query and then our view model is also ready next we will create view model Factory so create a new class under view model package as note view model Factory and done now what is V model provider Factory basically view model Factory is a class that instantiate and return view model simple so here I'll pass the parameter AS application and node repository then extend it as view model provider dot Factory and then inside it let me write the code first and then so this function is required to create an instance of the view model class basically it overrides the create method from The View model provider Factory interface then inside it it returns a new instance of the note view model by passing the application and note Repository instances to its Constructor this ASD is used to ensure that the return instance matches with the generic type D which we called before and that's it our notes VI mod Factory is also ready next go to main activity here we need to set up the view model which we created so to do that first initialize note view model then outside on create here create a functional setup view model and inside it let me quickly write the code and done see note repository initializes node repository object with node database instance then view model provider Factory creates a note view model Factory with parameters AS application and the note repository now once we have repository and Factory initialized then set it up in the note view model like this way view model provider with this and view model provider Factory which is used to create an instance of note view model then not view model class specify the class of the view model that needs to be retrieved and then lastly call set of view model method here inside the on Create and that's it our adapter view model Factory everything is ready now the only thing that is left is to set up fragments which we will cover in the next video stay tuned for that okay so yeah that is it for the video If you new to this channel then please consider subscribing to my channel and I'll see you in the next [Music] video
Info
Channel: Android Knowledge
Views: 1,775
Rating: undefined out of 5
Keywords: notes app android studio, note app android studio, notes app in andorid studio, notes in android studio, notes app in android studio using kotlin, mvvm notes app in android studio, notes application android studio tutorial, notes app android, notes app in android tutorial, how to make notes app in android studio, how to create notes app in android studio, notes app in android, notes app mvvm, android studio, how to create ui in android studio, notes app using room library, kotlin
Id: gL0L-RbfQyQ
Channel Id: undefined
Length: 14min 52sec (892 seconds)
Published: Fri Dec 22 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.