Java Android Room Database | Insert and Query | RecyclerView Example

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to my channel in today's video i'm going to show you how you can implement room database in your application and then we'll retrieve data and display displayed on the recycler view so we are going to use the room fully functional let's start with adding some dependencies as we needed for each project so i'm adding those so first of all we need room runtime set this one we need another dependency that is our room compile compiler okay spelling is rom add this one as i said like we are going to show it on a recycler view so let's add the recyclable dependency this one let's apply it and let our project to be sync now meantime let's add some packages so i'm creating a separate package for db where it is going to keep all the database related files so first of all we need a database class i'm giving it as the name app database another class we need for entity so we are going to use as a user and one more class we need that is the dao let's give it a name and it's the interface basically not the class user now so let's implement the app database so database class so first of all we need some annotations and main main thing is like this class should be the abstract class and inside it we need to pass our entity that is the user in our case so i'll implement user later first time implementing the database class and we need to give the version name currently i am giving it as a number one version number one so we can change it later whenever we make any change in the future so that time we can change it another thing we need the abstract func abstract function which is going to return the dao that's it we just need to define it and now we need to create a instance of our database class now we need a function which is going to return this instance this function need to take the input for the context so when this instance is null at that time only it is going to initialize that instance variable order creating a new variable so it's this simple concept of singleton you and the third parameter is our database name so you can give any name getting some error okay yeah allow in main thread and then build that's it i'm still getting an error let me see what we are missing okay first thing like version have two equals and i think second one is oh we forgot to extend this class i think yeah so we need to extend this class with room database there you go so that's it for this class or database class is ready to use let's move to the different user class our entity class and let's implement it get a name annotation as the entity and we i'm creating just two fields here and that is i'm going to give it a name as the first name last name and uid is going to be the auto increment like a primary key so we are going to just insert two values first name and last name and then we'll be retraining both the values and displaying it on the recycler view so let me give it as a primary key uid and it should be auto generated so that we don't need to insert it and it will be automatically insert and this is going to be the column info the column in our database give it a name any name you can give it what what you want to see in your database similarly for the last name yeah that's it for our entity now let's implement our interface user tab and give it annotation now first thing and then we need to simply write the function for um query insert delete update anything you want so i'm writing first of all the query which is simple query which is going to select all from the user table so it is going to return all the records so it must be the list type so let's see now so it is going to return the list of users and the function name is going to be get all user that's it now for similarly insert just need to give it a insert and then the function name void and give it a name insert user and it is going to take the object of user that's it for this and one more function delete i'll see if i can implement this delete function as well but all depends but i'm writing it in the it is also going to take then put as the user object our dav is also ready now so let's use them in our main activity so let's come up with the ui part so let me add a recyclerview in this main activity where we are going to display all the records from the database so i'm adding just one recyclerview and one button which is going to launch add new record activity where we are going to take the input from the user so recycler is done now i am adding a one button add new user button and its constraints and i'm giving it as a name also add new user yeah our ui is ready for main activity let's go and create one more activity so that is for the add new record so i'm giving it a name add new user activity and let's add the ui for that also layout so i'm adding here um to add a text one is to take the input for the first name and another one is to take four take the input for the last name and a button which is going to store uh insert which is going to call insert our dow that is going to insert the record both the values whatever user is going to insert in it so it's simple ui that's it and this button i am just giving id as we needed and setting up the constraints and giving it as a text as a display text save yep that's it for our this activity as well now let's implement this add new user activity first so first of all i'm creating a separate function save new user this is going to take the input so we need to read both the added text values so i extract the save button as well and now we need to handle it click let's handle the save button click and on the button click we need to call this function and this function is going to pass the value what user is going to enter in the first name edit text and the last name edit text you now we need to get the instance of database and call our insert function of tab so let's see how we can do that since it's the static function so we can call it by the name and simply passing the context and we can simply call insert user now we need to build the user object so create a user object and simply we need to set the first name what user entered and the last name and then pass this user object to insert function and once that the code is inserted then we will close this activity now let's go back to our main activity initialize our button like add new user button and on click of this button we'll call then that adding a new record activity so similarly i am handling its click now so i'm going to call it not the start activity i'm calling it start activity for results so as soon as one record is added then we need to reload our recycler view so i'm calling it startactivity4result for that we need to pass two parameter one is intent and the request code i'm passing a request code as 100 and in that case we need to override one function on activity results so when that activity get closed in that case one activity result will get called and we need to check the request code what we give there hundreds the same code will be there now we need to initialize our recyclerview so let me create a separate function for that so this is the good habit to create multiple functions so that your code looks neat and clean and it's easy to understand so this function is to get the records from the database the same thing what we did to insert but instead of insert function here we are going to call the get all users that was the query and is going to return the list of users so when we have list then we need to set it to the recyclerview so our most of the part is done now we just need to set our data to recyclerview so let's create a another function which is going to initialize the recycler view and setting the adapter and lot of stuff so now i'm retrieving the recyclerview from our layout now i need to set a linear layout manager so recycler view dots set layout manager and new linear layout manager and pass the context and for the separator in the recycler view you have seen one small line so that is known as item decoration so let's set that as well so that our each records look separated add item decoration and pass this divider item decoration now we need to set the adapter to recycler view set adapter but we don't have any adapter so let's create an adapter now get any name i am giving user list adapter now implement this adapter all right these functions of adapter creating a view holder class separate class a constructor for this my view holder now we need a layout as well how our recyclerview row will look like so let's create that as well one another layout so i'm keeping it as a linear layout itself i'm just adding two text view so that we just need to display a first name and last name so we don't need to worry so just simply adding two texts if we have more content like you have two three fields then you can add multiple text views here and simply you can add the multiple fields in your user entity so our layout is ready now use the same name here whatever we gave our text views there and keep it in the global variable so that we can access it from the onbindviewholder now we need to change your view holder yeah now we need a constructor for this adapter class so that we can get context so let's declare the context as a global variable but we need to initialize this context variable for that we need to have the constructor another thing we need the data the list of user so let's declare it as well and then we need to create a separate function for that so that users can set it this data from the activity so let's do that so this function set user list will call from the activity when we retrieve it and then we will set here if you are using mvvm then you can set this data from the view model itself and then call the notified data set so that all list get refresh and we need a constructor so that constructor will get the parameter context and this context can be initialized there we go now we need to implement these two functions as well on bind view holder and on create view folder and this item count this is simply going to return the size of our list now we need to inflate our layout the row layout row we have created so we need to inflate it here it is going to return over my view holder and pass this layout inflator view and here we need to set the value what we received from the outside just simply set this first name and last name similarly for the last name as well our adapter is ready to use now let's go back to our main activity and initialize it and set to our recycler view now we set the adapter to our recycler view but still we need to call the set list data so when we retrieve that inside the load user list function then we need to set our user list there we go and this load function will call at the launch of the main activity and when we are going to load when we are going to add any new record at that time that activity will close and this activity will reload so let's review our code we are almost done main activity yeah looks good to me okay yeah everything looks good to me so let's run it and see yeah run it and see so we don't have any record in our database so nothing is there let's add one record one user click on save there you go so you can retrieve it let's add new another record save it uh you can't see oh i think let me i know what's the issue let's go to our layout view so it's grabbing the whole screen record is there but it's grabbing the whole screen the one record so you need to set it to the wrap content instead of match parent yeah as i said record is already there but the first record was grabbing the whole screen so let me add one more record and see show you there you go guys that's all for today's video and thank you so much for watching if you have any question you can ask me in the comments uh comment section and you can find the code link in the description thank you so much please like subscribe and share thank you
Info
Channel: LearningWorldz
Views: 14,683
Rating: 4.858407 out of 5
Keywords:
Id: ONb_MuPBBlg
Channel Id: undefined
Length: 33min 9sec (1989 seconds)
Published: Tue Oct 20 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.