Welcome back.
And this video, we are going to make a couple of changes to our application allowing us to edit and
delete the entries.
OK, therefore, we will need to set up these two methods update record dialogue and delete record dialog.
I'm going to go over those in this video and there's a lot happening there, so it will be a longer
video and the general goal is to, well, pass them to our item adapter.
Let me see.
So too, the item adapter here, because in the last video, we uncommitted this item adapter stuff
that we had prepared before.
So this update listener as well as this delete listener.
So let's put them back in and let's put this update listener invoke back in as well and this one.
So we're going to invoke the method that we will use as a delete listener, which will be some sort
of a it will be a lambda right.
It will be a anonymous function.
And we're going to invoke that anonymous function, passing the idea of the individual item that we're
currently at in order to then allow to click on the update and the delete buttons.
So let's go ahead and implement those methods and therefore we will need to make a change to this item
adapter.
Don't be confused about the video not having this stuff here, because as I said, I cut the video a
little bit.
So the video is going to like this stuff, but we are going to have to now implement everything that
we need to add to the item adapter for it to work.
So let me comment this out.
For now, we are going to implement it in this video, however.
OK, so let's get started.
So let's set those two up.
Therefore, I'm going to comment this out for now, and let's prepare the update record entry.
So let's create this function update record dialog, which is going to be internally, then updating
our data.
So we need to get the ID of the item that we want to update, and we need to get the employee down in
order to make changes to our database.
Because the employee now, as you recall, has all of these functions like insert, update, delete
and so forth.
But in order to get access to it, we need to get the employee down object.
So we are going to use an update dialog.
Which is just going to be a dialogue object.
OK?
A dialogue with the context of this, and it's going to use the theme dialogue style.
OK, so there's this style said, Ah.
And let's see theme dialogue.
I think this one would be good theme.
Have competition.
And we need to import the dialogue class for this to work.
OK, now let's set up this update dialogue.
So let's give it a property of said Kent syllable.
So said Cancel Lobo.
Which is this one, and we're going to say that this is not going to be cancelled in the sense that
you cannot click away from it.
So if you want to update, clicking next to it will do nothing.
Only once you click on the update or the cancel button.
OK, so we say, said Constable Faults, and now we need to set up the binding object.
So binding will be our dialog update binding and we inflate our layout inflator here.
So basically, we're saying I want to use this dialogue update x l file this one here.
And because we're using view binding, we can access this XML file using the name of the XML file binding
and then we can inflate with the layout inflator.
Now we can use this dialogue binding for this XML file that we just looked at to set the content view.
So we're saying binding that route.
So what's happening here is if you look at it, it just sets the dialog to appear or to look like this
XML file so that this design here which we have created, which has this update record, name and surname
and so forth because we want to be able to edit it, right?
OK.
So now that we have set up how this dialogue is supposed to look like, let's take care of preparing
our data.
So when we click the update button, you see that the added texts are populated, so they're not empty.
We're not saying updated with a new entry.
We're saying it's updated with what we have in the entry already and now you can make changes to it.
So that's how a good updated functionality would work.
Of course, you could also not populate those two, but I want to populate those added texts with the
data that is inside of the database for that particular position.
So how do we get that?
Well, we can use our lifecycle scope again because this is stuff that has to run in the background.
So we need to use call routines here.
So we are inside of this quarantine scope and we can now use our employee down to call its methods.
And I'm going to call the method that will give me a particular entry.
So I'm going to get the employee by its ID, by passing the ID and collecting the data.
So I'm using the collect method here, which I can then get the data from our binding object.
So binding that had a text update name that set text.
And I can set the name here.
OK, so it, Missy.
So this collect method should give me the it.
If that's not the case, then I'm doing something wrong here, let me check.
So I'm dial fetch employee.
Thought collect.
Now, OK, so it was just an arrow there.
I don't know, maybe I misspelled something.
But now you see we get it, which is the employee entity.
So this is a lumber here.
You see, we get the employee entity, which is our employee object.
So if you look at it, it gives me the name of the employee entry.
OK, so now let's do the same thing with ITI update email ID, and this one will be also the sad text.
And here we need to use it or it email.
OK, so we're now assigning the email as well.
OK, so this is where we are populating the data here.
So this information into the added text.
So now let's add what should happen once we click on the update button this one here.
So let's use our binding object again.
That TV update to set on click listener.
So once we click on the update button, we want to execute some code, and this is the update button,
which is not really button, it's a text view, but it's fine.
I called the TV update, which is a text view update, which is this one here.
So if you look at the ID, it says TV update.
OK, so now here we can now get the name from those or we need to get this value.
We need to get this value.
So we get those from the edit text, right?
So the name, we get it from our binding that it update name, text to string.
And the same thing for the email, so email will be binding that he tee up the email ID, but text the
string, so we get the text and we make a string out of it because that's what we want to store inside
of this email variable.
So now we can check if those two aren't empty, because if the user for some reason entered an empty
entry, then we don't want to do anything.
So if name is not empty and our email is not empty.
Only then do we want to store something, so only then do we want to do a similar thing, as we did
with the create method you recall here where we inserted something and we, well, we need to close
the dialog at this point.
But here you see, we used to empty out inserts to empty your entity with the name and the email.
So let's do something similar here.
Let's use our employees Guidall.
But of course, this is again stuff that has to run in the background, so use a call routine again.
So here, let's use our employee down to call the update method at this update method needs to an employee
entity.
OK, employee entity object with the ID that we want to change the name that we want to change and the
email.
So here you see, the name and email is not enough.
We need to pass the ID as well because we want the only update one particular item, because if you
look at the update method, you see, well, you don't see much.
It's just an empty entity that we need to pass, which has those values.
So when you're creating, you don't need to add the ID because the IDs generate for you automatically.
But when you're making changes, you need to pass the ID.
OK.
So why are we doing all of this update stuff before even displaying the data?
Well, the problem is we can't display the data because our adapter uses the update stuff as well.
So if we would have not used to update listeners here, it would have been a lot easier.
So if we were to comment these two out and comment this part out here, we could get the data without
doing the update stuff.
OK, but we're doing everything at once.
So let's look at this.
So we have the employee.
Don't we now update the entry?
And now let's also let the user know that we updated the record.
So we're just displaying it via toast.
And let's make sure that the dialog that we have just opened is going to be dismissed because it's super
annoying if you have to update and then cancel too close to record.
So we want to dismiss the dialog as well.
And if they are empty, so if either of them is empty, I'm just going to have this else block here,
which is going to display name or email cannot be blank.
OK.
And now you see, we have two buttons that we have to update and the cancel button now let's also implement
the cancel button.
So let's say binding dot TV cancel.
So once we click on it, which means we need to add an account, click listener.
Let's dismiss the update dialog as well.
And of course, what's super important, we need to shoulder update dialogue at all.
OK, so this method will never be called because we are not setting up the data into our recyclables
so we can notice it until we have displayed everything.
So that's our update record dialog.
Now let's look at how we can have a delete dialog.
OK, we'll be very similar because if you look at it, well, it just this very basic dialogue here.
Super simple, yes, no delete record.
But it also needs to have the ID needs to know which idea we want to delete.
So let's created fun delete record alert dialog.
So we're using an alert dialog this time and not a custom dialogue that we created manually.
So with this record update record dialog that we created based on our own XML file.
But here we're not using that.
We're just using a default alert dialog.
So we need to pass the ID, which is of type integer, and we need to pass in the employee double in
order to well, then be able to make changes to the database.
So let's create the builder object, which will be the alert dialog builder.
With the context of this, then we need to set the title, so said title, which will be the delete
record title, which you see here, the lead record is just a title.
And then I need to set the positive button and the negative button, so this is the negative button,
this is the positive button.
So let's set those up.
So use the builder that set positive button.
And here we need to say which text it should display.
So I'm going to say yes, and now we can use this lambda in order to, first of all, get the dialog
interface here.
So if you look at it, we get the dialogue interface as well as another entry.
I'm not going to use that.
So this will be the lander and I'm going to use and now I can use my lifecycle scope.
Launch it again because here we're actually going to do the little delete thingy.
So once we click on the Yes button, then we want to delete.
So here I'm just going to use employee Dow to lead the employee entity.
Employee entity at the current ID, so that the idea that we get from calling the method will be passed
ID to the method itself.
And then we can also let the user know that we made the change, that we deleted this, so we make the
text here, we delete it and we need to show the post and then we also want to make sure that we close
the dialog interface.
So now if we click the no button.
Then I also want to this Mr interface, the dialogue.
OK, so here builders said negative button, I'm going to say, no, I'm going to get the dialogue interface
and the which which I'm not using so we can use to underscore here as well.
This is the lambda expression that I'm using and this missing the dialogue here.
So if you click on, no.
So now that we have set up the alert dialog builder.
So this builder and we can now set up the alert dialog itself, which is going to be of type alert dialog
and to can put like this, I can use my builder and call the Create Method, which will now create the
alert dialog.
And now I'm going to make this alert dialog.
Also not cancel lable.
So set cancel concealable to false.
And then let's display the alert dialog.
So show it.
Otherwise it won't do anything.
And now this function, we can make it private because no other class can use it anyway, so I should
use it.
Because they wouldn't have an employee Dow, I guess.
And the same goes, by the way, for the update record.
So private fund update record dialogue.
OK, so now that we have those methods ready, we can use them in and set up lists of data into our
recycler view.
So if the employee list is not empty, let's set up our item adapter.
We have the employee list.
We get that here.
OK, we get it passed to our method.
So here we now pass in the employee list.
And now what I'm going to do is I'm going to create a closure here where I get an integer, which is
the update ID.
So Update ID, I'm going to use this number again, use my update record dialog percent to update ID
as well as the employee, though.
All right.
And now.
Let's do the same thing with our final entry.
And that is the.
The lead listener.
OK, so the lead listener here, it's the delete idea that I get.
They lead I.D. again a London, and I'm calling the delete record alert dialog, I pass into the lead
ID.
And I use my employee, though.
Two now deleted.
So here basically, this is this entire quote here is basically just setting up this item adapter because
you see if we look at our item adapter, it requires an update listener with an I.D. and the lead listener.
So we used this idea that we're getting and we're calling the update record dialogue method.
This one here, which will then.
Take that idea and to empower you, Dole to update the current entry.
And the same goes for the Delete ID.
So we used that here as well to then delete the entry of using the delete record dialog.
So the dialog itself takes care of the deletion, which only happens once you click on the Yes button.
OK.
So now that we have the item adapter.
OK, at this point, we are going to implement these things as we had done before.
So just make sure that you have the item adapter with the correct settings here and set up all of this
binding stuff.
And also just quickly, because I'm not sure what I explained it correctly so this year is basically
we are creating what we need to pass to our item adapter, which you see is an update listener that
is a function that returns nothing.
OK, so we're basically creating this function call of like an anonymous function here where we used
the update aid that is given to us.
So we are getting the ID and we're making sure of that up here so where we are invoking it with the
ID.
So the update ID, the same goes for the Delete listener ID.
So we are invoking when we click on the items, we are invoking this method, which then invokes or
calls the update record dialog method, and it takes the update I.D. that we get from our item adapter
when we invoke it for the specific line that we are currently looking at.
OK, so that's what's happening here with this line as well as with this one here.
OK, now let's test this application and see what it's going to look like.
So we see here we have our Dennis Infowars.com and I can now delete the entry.
You see, it's gone.
So now let me add another entry here again, add record and click on the edit button, which unfortunately
doesn't work correctly yet.
So we have this update window wood, which opens up, but it's not working correctly.
So we are going to create our own style for that.
So I mean, the themes folder now and I unfortunately have to create my own style because otherwise
doesn't seem to work correctly.
So the style that I'm going to use, and this is really just the last part that we need to add to this
video then we're done.
I'm going to call this one theme dialog.
OK, and the parent of it will be the theme overlay app compared to dialogue.
OK, and now the style will have an item name of Android with major.
Let me see.
It's called Android when we know men with major.
OK, so Android calling this a property call like this, and I want to set that value to 90 percent
because I want to make sure that it takes 90 percent of the width of the screen and the minor should
also be 90 percent of the screen.
So I want to make sure that this dialogue is going to take 90 percent of the screen with otherwise,
it's just not going to do correctly.
So now we just need to use this theme dialogue that we just created.
So our style theme on the score dialogue, the one that was just created instead of our update record
dialogue function.
And now we should be golden and finally be able to update entries as well.
So let's go over here.
Let's update this.
You see, now it's taking 90 percent of the screen and it seems to work.
So the email address that I entered wasn't correct.
So let's use the correct one or a proper one.
Let's click on the update method, and you see the update has been done.
However, we still have one little while you could say security issue in our application, and that
is once we do click on the update method, we're not checking if the ID employee employer entity is
in fact empty or not.
So let's just make sure that it really isn't null.
Before we do the binding.
OK, before we find the employee entity to our updated features.
So if we run this again now, then we should see you me had something here at this record.
Let's make a change.
It's updated and now deleted, and you see our application doesn't crash.
Everything is good.
And now let's make a change to this one as well.
Update And now, well, not deleted.
It's now deleted and we see no records available.
OK, so now we have a running application which does all of the crude operations.
I know it's been quite a bit that we had to set up.
It's quite a bunch of code and we are using a bunch of concepts that we have not used before, like
flow, like a bunch of add annotations here using Dow and using couple routines a lot and room, and
it's quite a bit.
But the thing is, once you've done it once and you have the general structure for such an application,
you can now easily extend it.
And to then, the next time that you need to use a database, you can just use this as an example.
OK.
So that's it for this video.
Let's implement this into our seven minute workout app.