Welcome back to the fourth part of the sequel Élite series and this video, I would like to go over
to main activity in which everything comes together.
So everything that we've prepared so far, the database handler, the model class and item adapter,
and of course, all of the XML files that come together in this main activity, dot qty file.
So first of all, we, of course, have our uncreate.
Then we have this botnet set on Click Listener, which adds a new record, and then we have this set
up list of data into recycler view.
All right, let's look at this and record first at record is a function that I created in here.
And what it has, it takes the name from the header text.
So it takes this name here, that one from this added text.
Then it takes the email from this email, add a text and puts them into variables.
And then we have a database handler.
The database handler is the object of the database handler class, which we created.
So this one here.
So we create an object of the class in order to access its functionality.
All right.
We pass in the context because if we look at it, it needed a context when we wanted to create an object
of it.
So that's what we're doing here.
We are passing in this this is the context, which means this is our main activity.
Then we check if those variables aren't empty.
So those two added fields are the texts.
They may not be empty because if they are empty, then we will write a little tosing.
Name or email cannot be blank.
That's tried at record and then name or email cannot be blank.
OK, so that is that not as though what happens if it's not empty?
So if we enter something into both at a text, then we create this civil status which uses the ADD employee
method of our database handler, so of our database handler class.
And we pass in an object of employment class zero name email.
So here, if we look at ADD or if we look at this method, it's easy like this.
We will see that here it expects an employee or employee model, class object.
And then what it does is it inserts that entry into the table contacts.
OK, so it pretty much just adds to the database.
That's exactly what we want it right now.
You might wonder because here it says zero and zero.
I told you about the ID, which is this primary keep it will be automatically interconnected.
So the thing is, even though I enter zero here for employee model plus, the system itself will take
care of assigning the right ID.
So this will automatically get the right ID because it will be automatically incremented.
OK, and we will see that in a second.
We'll see that actually on the device.
Then we have the check here.
If the status is greater than zero, then things worked out.
Because I told you earlier, if the status was minus one, then that means that we get an error or that
we have an error.
And something went wrong when trying to add something to the database.
So if things went well, what we're going to do is just provide a little toast here.
Saved, saved for a long time and displaying that.
What I also do is I cleared the two textfield, so to edit text, so once I enter something here, someone.
Someone's email and here it's Simoni, so I wrote it incorrectly, but once I do that, you can see
the two edited texts are cleared and that is done by that clear but is clear method.
All right.
And then we can see this set up list of data into recycler view again.
So what is this method?
Because we're using it twice, right?
We used it already here in the uncreate method.
And we do it once again once we start something, so once we added the new entry.
Well, now let's look at this function because it's used to show the list of inserted data.
So it's pretty much updating the list that we see here at the bottom.
So this is a list of all the entries that we have in our database.
So we check is there an entry in the database or not?
And we do that by checking it items list, so let's check this out, get items this.
Gets an array list of employee morale class, so we use our database handler and then we get the employee
list.
So what this will do is it will just return database handler, view employee.
Now, of course, you could sum that up because that's what the ad tells me.
So let me do that.
It's a one liner, so it's a little short.
I got rid of the whole line.
So you're basically we're just returning the new employee.
That's what this get items list function does.
And you employee, as you might recall, it selects all employees from our table contacts.
So it will return all the entries that we have in the database.
OK, so that's what get us.
Let's go back to the function, though, so to set up lists of data we of you.
So we checked are there any entries, if there are some entries, then go into this culture, so then
do something.
If there are no entries, then go into this code.
So let's look at this code because it's shorter first.
So what happens is we say that our items list visibility is to set Cityview gone.
What that means is that this Harvey items list.
So basically, this list here is invisible and instead this text here, no records available is set
to visible.
So TV, no records available, visibility you visible.
So I just want to indicate that there is no entry to the user if there is actually no entry.
And we can see that once we delete all of those entries, we will see that this other text comes up.
But before I do that, I want to show you the entries in the database, actually.
OK, now let's look at this.
We said the Harvey item list to visible.
We made this other thing which says there is no entry to gone.
So we make it invisible and then we create a linear layout and we.
Create this item adapter, which gets the entries of our database, we get items list and then it puts
them into the RV items.
So we're just filling the adapter with the data that we have from the database.
And this is the method that is called just when we open up the application, so that's why it's called
in the uncreate method here.
And it's also called when we add another entry and will also be called if we delete an empty.
So at record.
We looked at that already.
Now let's look at update records, dialogue.
And we can see here that what we're doing is we go and create a new dialogue.
And by the way, this is just going to create this dialogue here, so edited and you can see updated
record, so it's basically creating this thing here.
So we create a new dialogue with a certain theme.
So we have the style here which are defined theme overlap, composite dialogue.
So it has a certain with height, so it doesn't take the full available height and so forth.
So these are details.
But let's look at this set cancelable.
We set that to false.
Then we set the content view to dialogue update and we looked at that before.
It's this dialogue here, updated record name, added text and so forth.
And then we set the text to whatever was in middle class, so that's why it's important.
And if we want to update something we need to pass in the object that we want to edit, then the same
goes for our email ID.
So basically what we're doing is when we click on EDIT, we want to know what we want to edit.
So we want to populate the values here to zero.
And here this test to test.
OK, we wanted to be an entry, otherwise we don't know what we are updating.
That's why it's important to have it here.
That's exactly what we populate here with those two lines.
So we are getting the value from the model.
So from our object and we are setting the text to our edit, text update, email ID.
And it is within our update dialog, which is this object that we created of type dialogue, which uses
our our layout dialog.
All right.
And then we create an set unclick listener.
So there are two buttons here.
And when we edit the update button at the cancel button, so when we update, we take the names and
the email.
So those two entries, we take those two entries and we store them in variables.
Then we create another object of our database handler.
Here, create an object of it and then we check if those values aren't empty, so what I'm doing here
is not is empty, but you can sum it up to is not empty.
So maybe that's a little better this way.
So we say if the name is not empty and the email is not empty because if one of them is empty, then
we don't want to update it.
So if both of them aren't empty, then called this method update employee and store the status.
So if it worked or not inside of this variable status, of course, we need to pass in an empty model
class with the name and the email.
Where do they come from?
Well, they come from.
Here, so.
The values that we put into this, these two fields, and then we, of course, also need to pass an
I.D., so we're just taking the ID of the element that we wanted to edit anyway.
So the idea of this record that we're currently updating.
So in this case that say this is an ID zero then to entry that we are editing.
All right.
And then if the status is greater than minus one, we know that it worked.
So we make a little toast here saying record updated.
We update the list.
So we update whatever we have here in this list here.
And then we dismiss the update dialogue.
So we get rid of this.
Dialogue here, this one.
OK, and, well, if we didn't feel both elements, so if we didn't feel both of those headed texts
and we get this toast here.
And then what shall happen if we press cancel on this dialogue?
This button here, well, we just dismissed the dialogue and then, of course, we may not forget to
update dialogue show because we are preparing everything up to that point up to here.
That's what the preparation of the update dialogue happens.
And this is where it actually is appearing or where it's actually shown.
All right, and then the delete record dialogue, so that's the dialogue that pops up once we try to
delete something.
So this one here.
Are you sure you want to delete zero?
So you can see even in the text, it's using the name of the entry and you can say no and yes.
Now let's look at this.
We create another builder, and that's something that we have done when we used other dialogues.
Maybe you can remember.
If not, then check out the alert dialog video once again.
So we use the builder, then we set the title, we set a message.
So title message, the icon, he set icon.
And then we say, what should happen if we press on the positive button?
We give it the name or text.
So we say yes here.
And here we create another database handler object, we have the status again, so we check if deleting
an employee.
And specifically the employee that we are passing in.
Works.
So what we're doing is we give in or pass and the idea of the employee model class object and we said
name and email to empty.
Then we check the status again.
So is this that is not for greater minus one because then everything worked out.
So we just let the user know and we update the data into respective you.
So let's say we update this list here, so let me delete something, you can see this list here is updated
and that's due to this call here, because if we don't call this, then the View will not know that
it has to be updated.
And of course, once you click on something, we want to dismiss the dialogue interface.
And the same goes if we press no, then we also want to dismissed or dismiss the dialogue in the face
dismissal.
And then we need to, of course, create that alert dialog.
We can set the cancelable to false or true, and then we finally show this dialogue.
OK, so this is the code.
Now let's look into how you can actually debug.
So let's say you want to see all the entries in your database, because here I am displaying them with
a bunch of code.
But what if you just want to see something on your database of the phone?
What you can do is you can go to your pilled gradle.
And here it's important, go into the module app and hear inside of the dependency, so you need to
scroll down a little bit.
You go inside of the dependencies and here you find this debug implementation.
I am Dr. Dino, that DB Inspector Colen three point four point one at a R, so that is what you need
to enter there, because if you do so and you run the application, what will happen is you will find
this application here.
So let me direct this up, you will find this squalid demo.
Application, which looks like a little database icon.
OK, once again, it's this icon right here, OK?
When you open it, you will find some database entries.
So on my device, I can see here comes Kukali demo databases, employee database.
So you can basically see it uses the folder entry data user zero, nothing too fancy here.
And then it uses the name of my.
Package Cellcom Sequela demo databases and then slash the name of the database and it's called employee
database, so it's called like I have defined right here.
So database name.
That's what I have defined here.
So if you want to see database name where it's used, it's actually used up here.
OK, so that's what we're passing to the secure light open helper and then we set it to employee database.
So employee database, and then we have this employee table, by the way, you can see that there is
something that we didn't create ourselves, this android on this or metadata.
So this is another one that is created.
But that's not something that we have done.
It's automatically there.
But there is this version, one that you can see here and Bridgit data table, the one we want standing
for database version one.
So whatever we entered here and then there is this table called employee table.
So that's exactly the name of the table that we have defined here.
And now let's look at this table.
So now you can see in this table we have an underscore ID and you can see there is one six, seven,
eight, nine and two, three, four or five.
They are all missing.
So that these are the ones that I have deleted.
And then you could see at zero or at one actually at ID one you have the name zero and the email test
test.
OK, now let's look at our application once again.
So let's move over and you will see that maybe it's a little small, but you can see here zero test.
Are you test.
OK, then the entry three seven.
So name three and email seven.
And then here's is at someone's email.
OK, so all of these entries are there.
Now let's delete this one and say yes and let's move over to our.
Database connection, you can see suddenly it's gone, so it has been deleted off our database, so
let's change this three to help, for example.
Let's move over, edit this three.
So hello.
Update.
And now let's go over to the database tool and you can see again here, it says hello.
So this is a way how you can debug your entries that you created in your database and your secure database.
And, yeah, now I think we make quite some progress.
And you can use that now.
And it is a long process.
There is a lot of stuff going on when using Scollard.
But the thing is, once you have set it up and you went through the process once and then maybe a second
time, it will really become natural after a while because it really is the same structure over and
over again.
The only thing that changes are the table names, the columns that you want to have in there, and then
where at which point you want to make those entries.
But that's pretty much it.
OK, so I hope you enjoyed this little journey of just using light.
And I know it's been a long one, but no worries.
It's going to be fine.
We're going to go and use this because then it will become a lot clearer once we use this in another
real world example and we're going to use it actually multiple real world example.
So we're going to use it in the seven minute workout app and also in the Happy Places app.
So see you in the next video.