How to Insert Data in Firebase Realtime Database Kotlin | Firebase Realtime Database Tutorial Kotlin

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone and welcome back in today's tutorial i will show you how you can insert data in firebase real time database using kotlin okay so as you can see here i have already added this to button on the main screen and if i click on this so nothing happens because these onset on click listener is empty so here i will show you i have created an insertion activity and i added three edit text view and one button as you can see here one for name edge etc and the button will be saving the data in database okay so in this activity i will initialize these views but first i want to add a intent here so when i click on this insert button so it will open this insertion activity so i will just simply create a variable here well intent where intent is equal to intent okay and i will pass this so this means there's uh this activity context or this means main activity dot kt okay and here and we need to pass the destination where we want to navigate the user so we we want to get the user to insertion activity so i will just simply type insertion activity uh class.java double comma sorry not command dot okay so and class.javas and here we need to start the activity so simply type start activity and enter and pass the intent here okay so whenever we click on this insert button so we will navigate to this insertion activity so let me show you if i run this application okay now if i click on this insert button so you will notice that this activity is open and this is our insertion activity so uh no we need to initialize our views here so i will uh do it like for private let a network so network means in kotlin that we will initialize this variable uh later so i will call it et employee name and this will be the type of electrics okay so uh when we're using uh java so we we we write like this edit text and then we we write the variable name like this and we initialize it but in kotlin we add the type like this okay so i will just simply uh copy this and i will paste it for three times more okay so for and i will change this uh name to employee h and i will change this name to employee salary okay employee salary oh sorry employee salary and i will change this to uh btn save data and i will change the type to button because this is a button not an edit text okay okay so uh we need we also need to create another variable for our database reference so let private letting it well and i will just simply call it db ref so it means database reference and it will be the type of database reference okay so database reference and uh i will just initialize this uh in the oncreate method here okay so first of all i will uh do the edittext so m uh employee name is equal to ndv type ndb this is the shortcut for uh find vy id so r dot id et employee name and i will just simply copy this to save some time so one three four okay so uh i will change this name to edge and i will also change this id to employee edge and i will change this to salary and this will be the salary and i will change this id of salary okay and i will change this to btn save data btn save okay this is our button id and also we will initialize our database reference here so db reference is equal to firebase database dot get instance dot get reference and here if you want to leave it empty so you can leave it empty but i will recommend you to give it a name so give it a name like i will give it a name of employees because we are adding employees data we are inserting employees data so uh in the future uh when we are featuring the data from our firebase database so we will need uh this reference this name so you you need to uh remember this name if you want to save this in a variable so you can save it so that's how you can remember it okay so our initialization is complete and now uh i will add a callback to the button so to add a call by convertible simply type the button variable name btn save data set on click listener okay inside this set on click listener we will call a method save employee employee data and this will be a function okay so uh if you click on this and then click on okay where is the red bulb okay there is no above right now but we can create it here like private private fund save data employee data and inside this uh function our method first we need to get uh the value from this uh edit text okay so whenever we type something like this so we need to get this text from this edit text on this button click and then after that we will add this text to our database okay so let me remove this text and to do it okay uh getting getting values okay so we will store all these values uh in a variable so uh employee name equal to [Music] et employee name dot text okay dot text to string okay well employee h is equal to 80 employee dot text to string okay and well employee salary will be equal to et employee salary dot text dot to string so basically what we are doing here we are getting the string values are anything we put in this edittext our plaintext uh we are getting it like by using dot text and we are converting it to string so if someone put one two three like the edge will be in uh integer and the cell will also be in integer so we are converting it to string okay so now we need to uh we need to validate our form so if i click on this button so nothing happens but uh when we implement the insertion method here of database and someone accidentally click on this button and the values are empty so the app will be crash okay so we need to check if employee name okay sorry name dot is empty r is empty so we need to add an error on this employee edit text name so set error okay set error and set error like this or simply this error equal to okay uh let me check on my previous code uh how i'm okay this is the error okay so i will just simply copy this from here because uh i just forgot this index okay dot error so i need to add the employee name here and please okay this is the same number name please enter uh enter the name enter name okay uh it is as it is okay so i need to uh copy it two times more for edge and for century okay so this will be employee h and this will change to 80 employee h and please enter h and this will be changed to the employee salary and also this will be changed to the employee salary and please enter salary okay so whenever a user click on button and the edit text is empty so uh the android app will show this this error on that edit text okay so now uh we need to create a unique id so uh on a uh whenever we enter some data of the user of the employee so we need to insert a unique ids or to avoid the overriding data okay so let's suppose uh some employees have same name same at same salary so uh we need to uh identify them by unique id so to create a generator and id so we will simply call it employee id employee id is equal to uh and this will be uh generated by the firebase database reference dot push okay dot push dot key okay so here okay we need to add the null check here okay and let me confirm it from here okay so database reverse that's good okay uh but but now we need an model class so we will just simply create a model class and we will just simply create a go to new cortland file and i will name it employee employee employee model okay and it would be data class okay so i will just simply type data here and this class will we will have no body and inside this we need okay data class so what is the error must have at least one primary constructor okay constructor data class employee so let me add the employee id and it will be a string okay string it will be null okay and where employee name so employee name and it will also be a string equal to null first of all we will initialize it as a null so employee employee edge it will also be a string if you want to change it to integer so you can change it it doesn't really matter employee employee salary will also be in string and okay you need to add these two dots okay null so this is employee okay employee okay and i will change it rename it refactor rename it employee effect right okay so now what we will do we create a variable okay a variable and we will call it employee employee is equal to employee model and here we need to pass our data so first of all we need to pass the employee id employee id employee name okay employee name here employee edge okay employee salary okay so our model is ready uh now we need to put this data in our firebase database so we need the database reference database reference dot child so the child means that we are creating a child uh by using this id so we will pass this id i will copy and paste it here and dot set values set values okay not values set value and we will pass the employee okay employee and also i will add a success listener or complete ad on complete listener add on complete listener okay so complete listener like this and i will show a toast here that data is uh inserted successfully so this okay and this will be there will be a message um data input inside this data insert it successfully okay and we will assign a toast length okay not comma toss dot and dot show okay and also we will add a failure add on so all these methods are uh from firebase database reference okay and i will call it simply error okay with arrow and i will simply uh copy this test and i will paste it here and i will simply type here error and we will can get in here here the error dot get error dot message okay so let's say our application is ready and now we will run it and we will check it if the data is researched but first of all we need to make some changes on our firebase database so let me finish this continue to console and we will go to the firebase real-time database okay and we will click on create database click next okay start in test mode but uh just leave as it is i will show you how you can change it from the database so now this is our database and we need to go to the rules and as you can see here the read and write rules are filed so you need to change it to true okay for testing your database because if these rule rules are files so we cannot write our read data to this database okay so simply change it to true and click on publish okay so this is a warning but you can ignore it for the time being and we will go to the data and then we will go to run application okay okay let me close this okay okay i will click on insert data okay so we have an error and what is the error okay default firebase app is not initialized okay so i have already googled it and you need to just simply add okay where it is okay here it is i think this we need to apply this plugin so if i zoom it in okay uh on the first video uh i think we forgot this plugin so just simply copy this okay i will put this link in the description go to your android project open your gradle the second one okay app level cradle okay and simply type here id and paste that plug in here google gms google service okay simply click on sync and now we will run our application and also our application will work perfectly okay close this [Music] okay now we will click on insert data and we will simply type the employee name so i will just simply type my name hello and my 26 and save me just simply one two three four okay and now uh i will click on save data save data and as you can see here data inserted successfully and here why it is called real-time database it is uh available here that is h that is name and let me add another record okay uh i will just simply type done h 12 and i will change the salary to something like 12 000 okay and uh notice here the data will be appear here as i say click on this save button okay as you can see here our data is here now okay and we need to clear this field when we click on this save button so how we will do it for the better programming you need to do to do this and we need to clear all these texts when the data is added okay e t e d name dot text dot player okay so we will just copy this two times okay and we will change this to h and we will change this to salary okay if i run the application now you will notice when we click on this data inserted all these electrics will be empty okay android studio shows that app is launched but we i think i have to run it again sometimes it happens okay so if i click on this and i will simply type camera on h 26 okay celery one two three four and if i click on this save button so you will notice the edit text are cleared now okay and if i go to database so here it is another record okay so that's it for this video guys and if you like this video please share this video with your friends and somehow if you want to uh implement uh the real-time database using java so you will find the link in the description i have already created a playlist which in which i'm using java [Music] for real-time database uh okay and if you like this video so again share this video with your friends and do subscribe to my channel and also um click on the bell notification option so you won't miss the upcoming videos
Info
Channel: CodingZest
Views: 27,953
Rating: undefined out of 5
Keywords: CodingZest, how to insert data in firebase database android, how to insert data in firebase realtime database, inserting data into firebase android, how to save data in firebase realtime database in android studio, how to save data in firebase realtime database, save data in firebase android studio, firebase realtime database kotlin, realtime database kotlin, android kotlin firebase tutorial, kotlin firebase app, kotlin firebase database, kotlin firebase database tutorial
Id: miJooBq9iwE
Channel Id: undefined
Length: 21min 5sec (1265 seconds)
Published: Mon Apr 04 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.