All right, so let's go ahead and finalize this application.
And therefore, I'm going to go very quickly over the user interface because it's a very generic user
interface.
You can see we have a text you with an ad, the text we have another text you with another added text
next to it.
Then we have this button, we have a text view and underneath we have this recycler view.
OK, so we're using a recycler view.
This is a scrollable view here.
You can see it when you, well, add enough items, you will see it even better, but you can even see
it from the animation.
And then we have this little item UI.
So this Item UI has one text view, which is the name another.
Text you with a little bit of distance, which is the email ID.
Then we have one button and the second button.
Which is more like an image view.
OK, so we're going to use an image view here instead of a button, but it's going to behave like a
button.
So that's generally the user interface.
You can try to build this yourself.
You will need to use an item adapter for it.
So this individual entry here is an item, so you will need to create an adapter that allows you to
store all of this information.
So I'm going to go over these parts of the video very quickly because we want to focus specifically
on the room aspects of it because everything else we have covered in the course so far.
So let's go ahead and jump right into it.
So first of all, what we need to do is we need to, of course, change the main activity XML file,
and I'm just going to paste in my court real quick and go over just very briefly.
So generally speaking, this is the user interface that we have.
You see, we have there's no records available, by the way.
Also, this is a text view that will only be visible if we have no entries in our database.
And there you see, so we have this text view, then the edit text text you add text button takes to
you and recycler view.
OK, so I'm using a constraint layout with this text view.
You can, by the way, also just download the XML file.
I would recommend to do that so that you don't have to type all of that stuff in, even though you could
use this as a little challenge for yourself.
So here I'm using an edited text instead of this entire added text lay out stuff that surrounds it because
I don't have a specific UI style here.
I'm keeping it simple just using edit text.
Then we have this other text view with the added text, which is just next to the text you email label.
So you see here starts to end off.
That's how you can get this.
Thing where you see that the texture is on the left of the edit text and then underneath we have this
button which has the idea of button and then we have this text which just says all the inserted records,
which is this bold text that you can see here, which is the top of it, is at the bottom of the button.
And then we have our recyclable, which is just underneath the text, you name.
And then finally, we have this text view, which is only visible if there are no records available,
which is at the bottom of the button, add.
OK, so that's pretty much the user interface.
And as I said, we also need to make sure that we have the individual items.
OK, so this item here you see, takes you, takes you image of your image view.
So all next to each other.
So let's go ahead and look over how this is going to look like.
So I'm going to import it here.
This is going to be how my role looks like.
You can see it's pretty empty because there are no images here because I also need to add the Dropbox
so you can download.
You can use your own troubles, but I'm just going to use those ones that you see here.
So the added icon, as well as the delete icon, like so.
OK, so now we have those two images here you see edit and delete.
We have a text view and to text you.
And how does this look like?
Well, I'm using a linear layout to keep it simple here.
Items are oriented horizontally, so right next to each other, not on top of each other.
We have a text.
You, another text you.
Another text you, which is, well, this text is just a column.
If you ever want to have it in there, you see name, call an email.
And then we have the two image views.
So one image you being our added image view and then the image view that listed the lead image view.
And then we have this little dialog that pops up when we want to edit something.
So you see up there.
Record is to text you at the top, then we have another text view.
This is an added text.
Another text to you with another added text.
And then we have two buttons here at the bottom, which one says update and the other one says Cancel.
OK, so we need to design this as well.
You can try this, of course yourself and pause the video.
But otherwise, let me just quickly put it in here.
So I'm going to call this one dialog update.
And this is going to be the code.
So I'm using a constraint layout once again with a text view, which is this title thingy.
So let's look over it.
It's this update record to text.
You mean, make it a little bigger.
Here on the side of that record, then we have this text here which says name, which is next to this
enter name added to text.
You see here they're connected to each other, so.
Start to start end of TBE name label.
Then we have this text you.
Which is underneath this other one button next to the added text, which is the enter email added text,
which is this one here?
And then we have this update button, which is also a text view I'm using and text you here and then
just cancel button.
But either way, you could have also just used buttons here.
Either way would have been fine.
And that's pretty much it.
OK, so now that we have the user interface out of the way, let's add some room code.
So first of all, we need to find a way to add a record because otherwise we don't have anything to
be displayed on the screen, right?
And we don't store anything in the database.
So if you don't have anything that you can retrieve or edit, then you cannot work with the database.
You need to add something first.
So let's go ahead and create a function add record here, which is going to be requiring an employee.
OK, so we need to pass the dowel that we want to work with, and now we can use view binding and therefore
we need to set up a binding object here at the very top.
Let's make sure we have that.
So I'm just going to use the activity.
Main binding said it to now by default, as always.
And then let's set up the binding and content view here accordingly, with standard multiple times in
the course already.
So activity mean binding inflate using the layout and flutter and setting the route as the content you.
OK, now we can use view binding in our ad record method.
And why do we need that?
Well, because in order to add something, we need to get the data that has been entered.
OK, so here.
This data, as well as this data, needs to be gotten as well as we need to add an outlook listener
to this ad record button.
So let's get the name first, which will using be using our binding object that at its takes name and
get its text property as a string of data string?
OK, and then we need to do the same thing with the email.
So here binding again, we get the added email ID or edit text email ID. And he'll get a text from it
to string.
OK, now we can check if both of them aren't empty, so if email is not empty.
And the answer in here, the name is not empty.
And maybe we should do it the other way around.
Email is not empty here just should be name just because of the order of how we created the variables.
It doesn't really matter.
But just for.
The looks of it, so if both of this is the case of both aren't empty, then we can insert data so we
can take this name and we can take this email and use our dole to insert something.
But because this has to be done in the background, so we need to use routine content here, we are
going to say that we want to use the lifecycle scope for that.
So we're launching our call routine.
OK, so here you need to import launch, which will then be available up here.
So we see lifecycle scope and quarantines launch have been both added to our namespaces up there and
now we can use to employee dole that has been passed to this ad record method in order to call its insert
method.
So if you recall, we had this insert method, the Suspend Fund Insert, which is a call routine function,
which we are now calling inside of the lifecycle scope.
And here we can now pastor data.
And what is it that we need to pass?
Well, we need to person and entity object.
So let's go ahead and create a template, the entity object like so where we can pass the idea, but
we can also skip it because it's set by default and also increment it because it is.
If we look at it, it is the primary key, so it will be auto generated.
We don't need to manually set the ID.
So the only thing we need to set is going to be the name and the email.
So let's set those two things.
Now we can also set up a text letting people know that the record was saved.
And then while we're at it, and by the way, I'm using the application context here, not just this,
because we are inside of this scope.
OK.
Otherwise, if you just use this, you can run into errors.
You see this will not work.
So we need to pass in the application context and then I can clear the entry.
So you see, if I enter Denis here and some email address and I add the records, you see that those
two fields are being emptied after.
So let's just do that real quick by using binding that added text name.
And let's use the text property, which could be another bill as well and clear it.
OK, and they'll do the same thing with our email.
So here email ID, text clear.
OK.
And then if those two are not not empty, so if either of those two entry fields so added tax is empty,
which means the variables that we have here are going to be empty, then we can just display a toast
saying email or name cannot be blank or name or email cannot be blank.
OK, now before we can test this, let's actually execute this ad record method at a point when we click
on the button.
OK, so we need to make sure that we implement the button otherwise.
Of course, nothing will work.
OK.
And now we need to call this add record method.
Once we click on the button, right?
So let's do that.
But the problem will be we will see in a second, but we don't have an amplitude now that we can pass
to it, at least not in the format that we need to get.
So let's use our binding button at set on Click Listener here in order to get started.
So here we can then call the add record and we can pass an employee dial.
But where do we get the employee dial from?
Well, that's the thing.
We don't have a gate, so we need to pass and plug it down to our add record method.
So here to do at call, add record with employee down.
OK.
So how do we set up this employee dome?
Therefore, we're going to create an application class and initialize the database.
OK, so let's go ahead and do that real quick here.
New and class, I'm going to call this one employee app.
And that will just be a class.
And that will inherit from application.
So it will be an application class now and here inside of it, we can create a DB.
Lazily.
Using our employee database.
Dot get instance.
And we need to pass this, which means this employee app application.
OK, so now we have this database that we can use.
For every application class, we must declare the app that we have set up in the Android Manifest as
well.
So let's go over to the Android Manifest and then it gets more and more tricky.
But that's just how it's done.
So now we can add the Android name property and use our employee app here with a dot at the beginning
here.
So Dot employee app and now it will use our employee app, which we can now set up in our main activity.
So here let's set up our employee Dow using our employee app.
So Employee Dow will be with our application as employee app.
This one here don't get its database object and use the employee Dow, which is this abstract function
that we created inside of our room database.
All right.
So what is going on here, because this one line, there's so much stuff going on in one line.
Let me go over to the activity so that what we need is we need an employee down to add a record, right?
Otherwise, we cannot store the data because we need to use the dollar in order to call its insert method,
which needs an employee entity, which we do have.
But we don't have the DOL itself yet.
So we need to have access to the DOL in order to get access to the Dow.
We need to set up an application which we just did here an application class, and then we can set up
the database in there to now have our database ready.
So here we are creating it lazily, which means that it loads the needed value to our variable whenever
it is needed.
So not directly, but only when it's needed.
OK, so that's the point about this lazy here.
And the database will then be the instance of our employee database that we have because we can only
have one instance, as we have covered in the last video.
So we have this companion object where we had this instance get instance method, which is a singleton
that only exists once in the entire application.
So there's only one instance of this instance object at once.
OK, so there's only one database in the entire application.
So now we can go ahead and get this instance by passing this as the application.
So because the get instance needs a context and we're passing the application context as the context
and we need to set up the application accordingly inside of our application tag of our Android manifest.
So not inside of an activity or anything because we are not using the database owning this activity,
we're using the database in the entire application.
And now in the main activity, we can go ahead and get the doll from the database.
So we need to get the application in order to get this DB object that we just created, which gets us
the database.
And then we can get its employee doll, which is this abstract function.
So the database that we're creating is our employee database, which is a room database, which we had
set up OK.
So.
At this point, we have our Dow and we can pass the Dow.
So let's call the add record method and pasta and buruji to it.
So now our application will know which door to use in order to use the insert method, which will take
an employee entity with a name and an email, and we'll store that in our application.
So quite a bit of stuff that you need to build around it.
Quite a bit of boilerplate code, but the thing is, this is quite safe.
So this is a safe approach.
You have to set it up once, but then from now on, it's going to be significantly easier.
So once you wrap your head around this, it's just going to be significantly easier because now you
can just use the doll that you have there and you can call its methods because the doll, if you look
at it, it has all of the methods that we need for crude operations.
So create.
Read.
Update and delete.
OK, so we have all the crude operations here and now let's actually run the application because what
we will see is that here in this app inspection here, there's a tap at the bottom and does app inspection
has a database inspector.
So now if I run the application and add an entry here, so let me add myself again.
Dennis with then is an email or dot com.
And now let's add the record and you will see that the database inspector has made the change.
So it added this employee database and it has this employee table.
Once you click into it, you will see the entries and let me make it a little bigger here.
You see, we have the ID one, which is Dennis with the email ID of Dennis at Infocom.
So now let's try this again with Frank and Frank at Tank dot com.
We add the records.
And the record will be added to our database, let me refresh it.
We see here two sides to Frank.
Frank Atlantic.com is in our database as well.
So this is inside of our employee database, inside of the employee table.
OK, so everything now is set up.
We're using our employee database.
We're using the employee entity, which contains the employee table.
So that's exactly the name that you see here.
OK, so this is where we defined the table name and now you see the table name.
So in here with all of its columns.
So these are the properties.
And you see this is the primary key which contains this key here as well.
So if you look at it, you can see here primary key, which was the integer which we called ID.
And then we have the name and we have the email.
But the email internally was called email ID.
So if you look at it, you see like in the table, it's called email ID.
Even though the name that we are using for our objects and to pass data to our objects and stuff is
going to be email.
So in the court, we're using email, but the actual column name is email ID, as you see here.
So we overrode that using this at column info where we passed a different name.
OK.
So this is pretty much how you can set it up.
You can also see, by the way, that here this is an integer.
This is a text property sold in the database.
Internally, it uses different names for the different data types that we have.
So it doesn't use int it uses integer like a full name.
It doesn't use string.
It uses text.
So SQL uses different wording here.
So that's pretty much it for how to add a record.
But in the next video, we're going to finalize the application because this is just going to be the
beginning of it.
As you see, it's quite a long video already.
So see you in the next one.