Hire we've done so now in this story we're going to go ahead and we're going to actually add a method
to this at Marshall or activity class in order to add a martial art object to a school like database.
So inside this class at martial art activity and after the end in the place of this uncreate method
here let's create a method.
So private Voyt and the name of this method is at martial art object at martial arts object to database.
So let's put up tariff practices here and that open and closed the method by using this codebreakers.
So first we want to actually get the value of the name of the martial art the price and the color from
these edit texts.
Right.
We don't want to hard code the value of these are the properties the name the price and the color.
You've got to get these values from these texts so I can say inside this Meffert let's create a variable
is strange the name of this variable is name value assignment operator and I can refer to this EDT name.
So it is the name that gettext to a string semicolon.
Let's create another ridable Stream.
And it is price value.
Assignment operator it is a price that gettext the two.
Semicolon strange color value assignment operator.
It is the color that just text that to string semicolon.
So now we have these variables right.
Is local value is actually inside this method.
Now let's actually use these values.
First of all let me actually run the application first.
Let's select this device and then click OK.
And I can see that our application is running on the simulator.
So here we have this menu item here right at Marshall and that's click on it.
Now we transitioned to this activity here Monserrat name Marshall Price Marshall of color.
So we are 100 percent sure that if the user enters a value inside this it takes we are going to actually
convert that value to sync value and thats okay.
But what about the price the price must be paid double.
If the user enters a value inside these ediface it is going to be converted to a string value.
Right.
Because we can see these rowdier But here price value right.
And if we cannot actually use that string value as a double value.
So we need to actually create a try catch here.
If the user enters For example a leader here for example if the user enters the leader here and if the
user for example wants to add the martial arts into the database if the application is going to crash.
So we need to actually catch the number format exception here.
There is an exception here.
So after these three lines of code here inside the Smurfette let's create a try catch block try and
open and close this try block by using these curly braces and then Cheche insert Apprentice's of this
catch block just open here.
Exception e open and close this catch block by using these curly braces and just type in that print
stack thrice inside is try block here.
I'm going to convert these price value which is currently of type screen.
OK.
Is the price string value.
I'm going to convert it to a double value so I can say double.
And then here I can see price double value price double value assignment operator and I can refer to
the double class double that parse double and then I just need to pass or value of type string to the
smith at post-bubble which is going to be price value semicolon.
If this line of code here goes wrong it's going to execute this catch block and our application is not
going to crash it's going to just execute.
It's going to just catch that exception and it's going to print something to the lock.
So that's how we can actually prevent our application to crash by using these tripe.
Tripe like statements this try catch blocks.
So after this line of code here because we want to create or marshal or object and at that marshal object
to our school database here we need to create a variable of type marshal.
So I can say martial arts and the name of this valley is martial art object assignment operator.
So let's say new martial art.
So here we are creating an object of a martial art.
First we need to pass the ID name price and the color so we know that the ID is actually the primary
key auto increment.
So we don't care about the id the ID is going to be specified automatically and it is going to be incremented
automatically for us whenever we create a new object.
So for the ID just pass the value zero comma.
Now its time to specify the value for the name.
So we have a variable for the name here right name value comma.
Now we need to specify a value for the price so I can just pass this price double value here comma and
finally a value for the color value.
Thats a semicolon here.
So name price color.
So thats why we created these variables here.
And we actually dont care about the ID so that it is on Marshall objects and it may create a new line
here so that you can see these values better.
So now that we have this motional object its time to add this monster that object to our database.
First it will have actually a reference an instance of type database handler class.
So let's scroll to the top here.
Below this line of code that actually declares these buttons I can't see database handler and the name
is database handler semicolon.
Let's go into this uncreate Smurfette and after these lines of code I can initialize my database handler
instance.
So just refer to this instance to disvalue but as I'm an operator new database handler and here we just
need to pass the context which is going to be at Marshal activity that this semicolon.
So not just that our Insight is a database handler class we created or constructed.
If you remember here.
So this is all constructed and we said that this constructor accepts an argument of type context.
So you can see that here.
So let's go back to our outmuscle of activity class.
And here we actually declare this variable of type database handler class.
And inside these are created if for be initialized this variable.
He had new data of his handler and it has this context now that we have this instance of a database
handler inside this tripe block after creating this monster that object I can refer to this instance
database handler and I can call the Smurfit at martial arts at Martial Art.
And we just decide if it needs to and it's an argument of like Marshall Marshall object.
Right.
If I go to the state of his handler class you can see that we have the Smurfette at martial art in order
to add a martial object to our school like database.
So let's go back to our activity at much of the activity class and after this line of code I can create
a toast message in order to let the user know that this martial art object is actually saved in a school
database so toast that makes text first we need to pass the context which is going to be at the Monserrat
activity that this come on.
And that I mean to specify the takes.
And here I can say this emotional object is a martial art object is saved to as few light database.
OK come on.
And then we need to specify the duration which is going to be short that show.
Finally you need to call them short Meffert here on this object possumus close.
So let me create a new line here so that you can see these values better.
So now let's run the application.
And let's see what happens.
Let's select this emulator.
Let me show you the emulator now so you can see that our application is running on the simulator let's
click on at martial art.
So for the martial art name let's say boxing martial art price for example 1000 martial art color.
For example I just passed the value read.
So let's clear cut at martial art as you can see nothing happened here.
Why.
Because first I want to actually I want you to pause the video and figure out the problem and when you're
ready just continue with the video.
Because here there is a technical problem here.
We created this method here at martial art objects right.
But we didn't actually call this method from anywhere inside our class.
Right.
So we can see that we have two buttons or you want this martial art object to to be added to the database
when the user actually clicks on this button when the user touches this outmuscled button.
So let's go to Android studio and here.
Let's actually create a listener for our buttons.
So after this line of code that actually creates a new instance of type database handler.
OK I'll refer to this PTEN at martial art that sits on click listener and I just to pass the context.
The context is at Marshal activity that this lets also create a listener for our PTEN patch.
So Pythian back to set and click listener now and then here I just need to actually pass the context
so it is going to be at martial arts activity to this.
But here because we said that this class at Martial activity class is going to be the listener for the
clicks or the touches that the user actually implements on the button.
We need to this class must implement the view that uncle is an interface.
Otherwise the compiler is going to complain.
You can see that we have errors here so let's scroll to the top here.
After this line of code here you can see that it says at Marcheline activity extends apcupsd activity
implements view that unclick listener right.
So because this class is implemented in this interface view that unclick listener diskless must provide
an implementation for the unclear method.
So let's scroll down here.
And before the end in place of this class here this martial art is actually at Martial of activity class
or just to type in hit and click and you can see that the autocomplete feature of Android studio is
helping us to choose the Smurfette so select it and then personated on your keyboard.
Inside is Uncle Meffert because we have two buttons.
We need to differentiate between these buttons.
So let's first change these arguments name to view.
And here I can create a switch statement.
Right so each and here I can refer to the view that gets ID.
So let's open and close this switch statement by using the curly braces case.
And here I can see all that ID that PTEN at martial arts.
Colon.
OK.
And then here let's create a new line here.
So here I am saying that switch between the values of the ID of this view also view is the user interface
component that is actually the tree getting to this unclick method.
OK.
When the user touches the button the button is a subclass of view.
Right.
So any use that has this spot in this argument here is the user interface component or the button that
is actually triggering the action.
So here we are checking that if the view that good ID is actually PTEN martial arts in that case we
are going to call this method this method that he created.
He had an alternate object database so I couldn't see at Marshall that object to that of his approach.
Right.
So let's run the application now.
Let me show you the emulator.
Let's click on at martial art.
So for the martial arts name I can't say again boxing martial art.
Price one thousand martial art color red.
Let's going to court martial art.
Now it says this martial art object is saved to a school like stop a slide.
So now when the user actually clicks or touches this back button I want this activity to be actually
eliminated.
So let's go back to android sitio.
And here you can see that we have the switch statement and we here we have this case right.
So let's create another case statement case our thoughts ID that PTEN go back colon and this time we
want to actually finish.
We've got to call the finish my faith on an instance of outmuscle activity.
So it is actually the start to finish this finish.
This refers to an instance of type martial art activity.
So when I say this that finish this activity is going to actually be eliminated and the activity below
this activity which is actually the main activity is going to be shown to the user.
Right.
So now I can say bridge.
Let's run the app now.
Let me show you the emulator let's look at martial art if I click on patch.
Now you can see that we have this activity here.
Let's actually add another functionality to this application.
Let's go to the Android studio and inside this martial art class I can override the twisting method
that this class actually inherits from the object class.
So let's scroll down here and here I can say two streams.
So let's delete this value here written superlative string here because I want to put my own implementation
of this method here.
So now I can say return and here you can see that we have this instance variables these getters and
setters right.
So I can't say name price and color.
I want to return these values.
So first I want to return the name return.
Get Marshlack name plus two coats for a slash in a newline character plus get martial art price plus
two coats by Slash and plus get martial art.
Actually color semicolon.
So let's go to our martial art activity class and here for this Toastmasters I can't delete this value
here.
I can say actually when I refer to the smart object here I want to out with the string value of this
object to the screen.
The string method of this object is going to be called automatically right because I want to convert
this value to a string value so the string method of this object is going to be called.
So here I just need to say I just need to put on a concatenation operator.
Here are two quotes.
And here I just need to put a space so I can't say Marshall Marshall object martial art object is added
to database.
Let's run the application now.
Let me show you the emulator.
So let's click on at martial art for the martial art name.
Just type in here.
Boxing martial art.
Price 2000.
Martial Art.
Color Blue.
Let's go.
So let's click on.
At martial art now it's boxing.
These values is added to the database.
If I click on it again.
Now you can see that another object with these values is added to that database.
If I click on it again you can see that message here.
So that's it.
And I think that's enough for the search for you and for this activity at Marshal activity in the next
story we're going to actually work with other activities of this application.
So if you have any questions about the concepts that I taught you in this lesson please post the discussion
in the course and then I do my best science and all of your questions.
Thank you very much for watching and I'll see you in the next story.