1
00:00:00,280 --> 00:00:07,980
Hire we've done so now in this story we're going to go ahead and we're going to actually add a method

2
00:00:08,490 --> 00:00:15,280
to this at Marshall or activity class in order to add a martial art object to a school like database.

3
00:00:15,540 --> 00:00:21,630
So inside this class at martial art activity and after the end in the place of this uncreate method

4
00:00:21,690 --> 00:00:23,880
here let's create a method.

5
00:00:24,120 --> 00:00:36,360
So private Voyt and the name of this method is at martial art object at martial arts object to database.

6
00:00:36,360 --> 00:00:41,790
So let's put up tariff practices here and that open and closed the method by using this codebreakers.

7
00:00:41,790 --> 00:00:48,120
So first we want to actually get the value of the name of the martial art the price and the color from

8
00:00:48,120 --> 00:00:49,280
these edit texts.

9
00:00:49,320 --> 00:00:49,950
Right.

10
00:00:50,010 --> 00:00:56,840
We don't want to hard code the value of these are the properties the name the price and the color.

11
00:00:56,970 --> 00:01:02,810
You've got to get these values from these texts so I can say inside this Meffert let's create a variable

12
00:01:02,840 --> 00:01:11,100
is strange the name of this variable is name value assignment operator and I can refer to this EDT name.

13
00:01:11,100 --> 00:01:17,430
So it is the name that gettext to a string semicolon.

14
00:01:17,870 --> 00:01:19,970
Let's create another ridable Stream.

15
00:01:20,360 --> 00:01:21,990
And it is price value.

16
00:01:23,260 --> 00:01:28,610
Assignment operator it is a price that gettext the two.

17
00:01:28,780 --> 00:01:35,660
Semicolon strange color value assignment operator.

18
00:01:36,250 --> 00:01:42,230
It is the color that just text that to string semicolon.

19
00:01:42,240 --> 00:01:43,900
So now we have these variables right.

20
00:01:43,900 --> 00:01:47,130
Is local value is actually inside this method.

21
00:01:47,130 --> 00:01:51,090
Now let's actually use these values.

22
00:01:51,090 --> 00:01:54,540
First of all let me actually run the application first.

23
00:01:54,630 --> 00:01:56,900
Let's select this device and then click OK.

24
00:01:56,960 --> 00:01:59,890
And I can see that our application is running on the simulator.

25
00:01:59,910 --> 00:02:04,480
So here we have this menu item here right at Marshall and that's click on it.

26
00:02:04,530 --> 00:02:09,870
Now we transitioned to this activity here Monserrat name Marshall Price Marshall of color.

27
00:02:10,170 --> 00:02:16,410
So we are 100 percent sure that if the user enters a value inside this it takes we are going to actually

28
00:02:16,410 --> 00:02:19,550
convert that value to sync value and thats okay.

29
00:02:19,800 --> 00:02:23,290
But what about the price the price must be paid double.

30
00:02:23,400 --> 00:02:28,380
If the user enters a value inside these ediface it is going to be converted to a string value.

31
00:02:28,410 --> 00:02:28,710
Right.

32
00:02:28,710 --> 00:02:31,590
Because we can see these rowdier But here price value right.

33
00:02:31,620 --> 00:02:35,770
And if we cannot actually use that string value as a double value.

34
00:02:35,940 --> 00:02:40,350
So we need to actually create a try catch here.

35
00:02:40,560 --> 00:02:46,470
If the user enters For example a leader here for example if the user enters the leader here and if the

36
00:02:46,470 --> 00:02:52,230
user for example wants to add the martial arts into the database if the application is going to crash.

37
00:02:52,230 --> 00:02:56,300
So we need to actually catch the number format exception here.

38
00:02:56,460 --> 00:02:57,690
There is an exception here.

39
00:02:57,720 --> 00:03:05,490
So after these three lines of code here inside the Smurfette let's create a try catch block try and

40
00:03:05,570 --> 00:03:11,330
open and close this try block by using these curly braces and then Cheche insert Apprentice's of this

41
00:03:11,330 --> 00:03:13,740
catch block just open here.

42
00:03:13,760 --> 00:03:19,790
Exception e open and close this catch block by using these curly braces and just type in that print

43
00:03:19,850 --> 00:03:23,320
stack thrice inside is try block here.

44
00:03:23,370 --> 00:03:28,190
I'm going to convert these price value which is currently of type screen.

45
00:03:28,220 --> 00:03:28,590
OK.

46
00:03:28,650 --> 00:03:30,770
Is the price string value.

47
00:03:30,800 --> 00:03:33,920
I'm going to convert it to a double value so I can say double.

48
00:03:33,980 --> 00:03:43,310
And then here I can see price double value price double value assignment operator and I can refer to

49
00:03:43,310 --> 00:03:48,890
the double class double that parse double and then I just need to pass or value of type string to the

50
00:03:48,890 --> 00:03:52,560
smith at post-bubble which is going to be price value semicolon.

51
00:03:52,610 --> 00:03:57,710
If this line of code here goes wrong it's going to execute this catch block and our application is not

52
00:03:57,710 --> 00:03:59,690
going to crash it's going to just execute.

53
00:03:59,690 --> 00:04:03,520
It's going to just catch that exception and it's going to print something to the lock.

54
00:04:03,620 --> 00:04:08,720
So that's how we can actually prevent our application to crash by using these tripe.

55
00:04:08,810 --> 00:04:12,200
Tripe like statements this try catch blocks.

56
00:04:12,230 --> 00:04:17,930
So after this line of code here because we want to create or marshal or object and at that marshal object

57
00:04:17,930 --> 00:04:22,240
to our school database here we need to create a variable of type marshal.

58
00:04:22,280 --> 00:04:29,610
So I can say martial arts and the name of this valley is martial art object assignment operator.

59
00:04:30,150 --> 00:04:33,510
So let's say new martial art.

60
00:04:33,540 --> 00:04:36,570
So here we are creating an object of a martial art.

61
00:04:36,600 --> 00:04:43,530
First we need to pass the ID name price and the color so we know that the ID is actually the primary

62
00:04:43,530 --> 00:04:45,070
key auto increment.

63
00:04:45,180 --> 00:04:50,850
So we don't care about the id the ID is going to be specified automatically and it is going to be incremented

64
00:04:51,360 --> 00:04:54,470
automatically for us whenever we create a new object.

65
00:04:54,600 --> 00:04:58,100
So for the ID just pass the value zero comma.

66
00:04:58,110 --> 00:05:01,220
Now its time to specify the value for the name.

67
00:05:01,410 --> 00:05:05,830
So we have a variable for the name here right name value comma.

68
00:05:06,120 --> 00:05:13,680
Now we need to specify a value for the price so I can just pass this price double value here comma and

69
00:05:13,680 --> 00:05:16,310
finally a value for the color value.

70
00:05:16,520 --> 00:05:19,000
Thats a semicolon here.

71
00:05:19,170 --> 00:05:21,300
So name price color.

72
00:05:21,360 --> 00:05:24,280
So thats why we created these variables here.

73
00:05:24,270 --> 00:05:30,420
And we actually dont care about the ID so that it is on Marshall objects and it may create a new line

74
00:05:30,420 --> 00:05:32,280
here so that you can see these values better.

75
00:05:32,280 --> 00:05:38,380
So now that we have this motional object its time to add this monster that object to our database.

76
00:05:38,390 --> 00:05:43,520
First it will have actually a reference an instance of type database handler class.

77
00:05:43,560 --> 00:05:45,080
So let's scroll to the top here.

78
00:05:45,100 --> 00:05:50,610
Below this line of code that actually declares these buttons I can't see database handler and the name

79
00:05:50,760 --> 00:05:52,500
is database handler semicolon.

80
00:05:52,770 --> 00:05:58,530
Let's go into this uncreate Smurfette and after these lines of code I can initialize my database handler

81
00:05:58,590 --> 00:05:59,230
instance.

82
00:05:59,250 --> 00:06:04,830
So just refer to this instance to disvalue but as I'm an operator new database handler and here we just

83
00:06:04,830 --> 00:06:10,740
need to pass the context which is going to be at Marshal activity that this semicolon.

84
00:06:11,190 --> 00:06:15,990
So not just that our Insight is a database handler class we created or constructed.

85
00:06:16,020 --> 00:06:17,310
If you remember here.

86
00:06:17,300 --> 00:06:22,500
So this is all constructed and we said that this constructor accepts an argument of type context.

87
00:06:22,500 --> 00:06:23,520
So you can see that here.

88
00:06:23,520 --> 00:06:26,260
So let's go back to our outmuscle of activity class.

89
00:06:26,430 --> 00:06:30,310
And here we actually declare this variable of type database handler class.

90
00:06:30,450 --> 00:06:33,540
And inside these are created if for be initialized this variable.

91
00:06:33,580 --> 00:06:39,180
He had new data of his handler and it has this context now that we have this instance of a database

92
00:06:39,180 --> 00:06:44,250
handler inside this tripe block after creating this monster that object I can refer to this instance

93
00:06:44,610 --> 00:06:50,120
database handler and I can call the Smurfit at martial arts at Martial Art.

94
00:06:50,220 --> 00:06:54,520
And we just decide if it needs to and it's an argument of like Marshall Marshall object.

95
00:06:54,540 --> 00:06:54,900
Right.

96
00:06:54,900 --> 00:07:00,750
If I go to the state of his handler class you can see that we have the Smurfette at martial art in order

97
00:07:00,750 --> 00:07:03,470
to add a martial object to our school like database.

98
00:07:03,690 --> 00:07:09,300
So let's go back to our activity at much of the activity class and after this line of code I can create

99
00:07:09,360 --> 00:07:14,890
a toast message in order to let the user know that this martial art object is actually saved in a school

100
00:07:14,950 --> 00:07:20,970
database so toast that makes text first we need to pass the context which is going to be at the Monserrat

101
00:07:21,000 --> 00:07:23,990
activity that this come on.

102
00:07:24,360 --> 00:07:25,730
And that I mean to specify the takes.

103
00:07:25,740 --> 00:07:41,560
And here I can say this emotional object is a martial art object is saved to as few light database.

104
00:07:41,730 --> 00:07:42,640
OK come on.

105
00:07:42,860 --> 00:07:47,050
And then we need to specify the duration which is going to be short that show.

106
00:07:47,090 --> 00:07:51,130
Finally you need to call them short Meffert here on this object possumus close.

107
00:07:51,450 --> 00:07:54,560
So let me create a new line here so that you can see these values better.

108
00:07:54,560 --> 00:07:56,360
So now let's run the application.

109
00:07:56,380 --> 00:07:57,530
And let's see what happens.

110
00:07:57,560 --> 00:07:59,120
Let's select this emulator.

111
00:07:59,120 --> 00:08:03,170
Let me show you the emulator now so you can see that our application is running on the simulator let's

112
00:08:03,170 --> 00:08:04,920
click on at martial art.

113
00:08:05,120 --> 00:08:11,480
So for the martial art name let's say boxing martial art price for example 1000 martial art color.

114
00:08:11,540 --> 00:08:13,470
For example I just passed the value read.

115
00:08:13,490 --> 00:08:18,460
So let's clear cut at martial art as you can see nothing happened here.

116
00:08:18,470 --> 00:08:18,880
Why.

117
00:08:18,950 --> 00:08:25,010
Because first I want to actually I want you to pause the video and figure out the problem and when you're

118
00:08:25,010 --> 00:08:26,450
ready just continue with the video.

119
00:08:26,450 --> 00:08:29,210
Because here there is a technical problem here.

120
00:08:29,300 --> 00:08:32,820
We created this method here at martial art objects right.

121
00:08:33,020 --> 00:08:38,240
But we didn't actually call this method from anywhere inside our class.

122
00:08:38,240 --> 00:08:38,870
Right.

123
00:08:38,930 --> 00:08:45,860
So we can see that we have two buttons or you want this martial art object to to be added to the database

124
00:08:46,190 --> 00:08:51,010
when the user actually clicks on this button when the user touches this outmuscled button.

125
00:08:51,020 --> 00:08:53,520
So let's go to Android studio and here.

126
00:08:53,750 --> 00:08:57,750
Let's actually create a listener for our buttons.

127
00:08:57,770 --> 00:09:03,570
So after this line of code that actually creates a new instance of type database handler.

128
00:09:03,770 --> 00:09:10,140
OK I'll refer to this PTEN at martial art that sits on click listener and I just to pass the context.

129
00:09:10,160 --> 00:09:16,610
The context is at Marshal activity that this lets also create a listener for our PTEN patch.

130
00:09:16,610 --> 00:09:24,260
So Pythian back to set and click listener now and then here I just need to actually pass the context

131
00:09:24,260 --> 00:09:28,390
so it is going to be at martial arts activity to this.

132
00:09:28,640 --> 00:09:35,840
But here because we said that this class at Martial activity class is going to be the listener for the

133
00:09:36,110 --> 00:09:39,890
clicks or the touches that the user actually implements on the button.

134
00:09:39,890 --> 00:09:45,860
We need to this class must implement the view that uncle is an interface.

135
00:09:45,890 --> 00:09:47,700
Otherwise the compiler is going to complain.

136
00:09:47,750 --> 00:09:50,970
You can see that we have errors here so let's scroll to the top here.

137
00:09:51,020 --> 00:09:56,060
After this line of code here you can see that it says at Marcheline activity extends apcupsd activity

138
00:09:56,750 --> 00:10:01,870
implements view that unclick listener right.

139
00:10:01,880 --> 00:10:09,230
So because this class is implemented in this interface view that unclick listener diskless must provide

140
00:10:09,260 --> 00:10:11,440
an implementation for the unclear method.

141
00:10:11,690 --> 00:10:13,030
So let's scroll down here.

142
00:10:13,160 --> 00:10:19,970
And before the end in place of this class here this martial art is actually at Martial of activity class

143
00:10:20,290 --> 00:10:24,680
or just to type in hit and click and you can see that the autocomplete feature of Android studio is

144
00:10:24,680 --> 00:10:28,020
helping us to choose the Smurfette so select it and then personated on your keyboard.

145
00:10:28,040 --> 00:10:30,700
Inside is Uncle Meffert because we have two buttons.

146
00:10:30,700 --> 00:10:33,140
We need to differentiate between these buttons.

147
00:10:33,200 --> 00:10:35,840
So let's first change these arguments name to view.

148
00:10:35,900 --> 00:10:38,990
And here I can create a switch statement.

149
00:10:38,990 --> 00:10:43,430
Right so each and here I can refer to the view that gets ID.

150
00:10:43,910 --> 00:10:48,150
So let's open and close this switch statement by using the curly braces case.

151
00:10:48,170 --> 00:10:53,230
And here I can see all that ID that PTEN at martial arts.

152
00:10:53,240 --> 00:10:54,090
Colon.

153
00:10:54,590 --> 00:10:54,970
OK.

154
00:10:55,100 --> 00:10:57,650
And then here let's create a new line here.

155
00:10:57,650 --> 00:11:04,040
So here I am saying that switch between the values of the ID of this view also view is the user interface

156
00:11:04,040 --> 00:11:07,490
component that is actually the tree getting to this unclick method.

157
00:11:07,520 --> 00:11:08,050
OK.

158
00:11:08,060 --> 00:11:11,840
When the user touches the button the button is a subclass of view.

159
00:11:11,840 --> 00:11:12,200
Right.

160
00:11:12,320 --> 00:11:18,470
So any use that has this spot in this argument here is the user interface component or the button that

161
00:11:18,470 --> 00:11:20,210
is actually triggering the action.

162
00:11:20,210 --> 00:11:26,690
So here we are checking that if the view that good ID is actually PTEN martial arts in that case we

163
00:11:26,740 --> 00:11:29,780
are going to call this method this method that he created.

164
00:11:29,780 --> 00:11:35,240
He had an alternate object database so I couldn't see at Marshall that object to that of his approach.

165
00:11:35,300 --> 00:11:35,720
Right.

166
00:11:35,720 --> 00:11:37,730
So let's run the application now.

167
00:11:37,790 --> 00:11:39,100
Let me show you the emulator.

168
00:11:39,200 --> 00:11:40,730
Let's click on at martial art.

169
00:11:40,760 --> 00:11:45,110
So for the martial arts name I can't say again boxing martial art.

170
00:11:45,110 --> 00:11:48,520
Price one thousand martial art color red.

171
00:11:49,010 --> 00:11:50,440
Let's going to court martial art.

172
00:11:50,540 --> 00:11:55,300
Now it says this martial art object is saved to a school like stop a slide.

173
00:11:55,370 --> 00:12:03,830
So now when the user actually clicks or touches this back button I want this activity to be actually

174
00:12:03,830 --> 00:12:04,400
eliminated.

175
00:12:04,400 --> 00:12:06,650
So let's go back to android sitio.

176
00:12:06,890 --> 00:12:12,080
And here you can see that we have the switch statement and we here we have this case right.

177
00:12:12,110 --> 00:12:21,010
So let's create another case statement case our thoughts ID that PTEN go back colon and this time we

178
00:12:21,020 --> 00:12:22,260
want to actually finish.

179
00:12:22,280 --> 00:12:26,760
We've got to call the finish my faith on an instance of outmuscle activity.

180
00:12:26,780 --> 00:12:30,310
So it is actually the start to finish this finish.

181
00:12:30,410 --> 00:12:33,270
This refers to an instance of type martial art activity.

182
00:12:33,620 --> 00:12:41,210
So when I say this that finish this activity is going to actually be eliminated and the activity below

183
00:12:41,210 --> 00:12:45,700
this activity which is actually the main activity is going to be shown to the user.

184
00:12:45,890 --> 00:12:46,440
Right.

185
00:12:46,790 --> 00:12:49,760
So now I can say bridge.

186
00:12:50,010 --> 00:12:51,460
Let's run the app now.

187
00:12:52,500 --> 00:12:58,260
Let me show you the emulator let's look at martial art if I click on patch.

188
00:12:58,290 --> 00:13:01,290
Now you can see that we have this activity here.

189
00:13:01,380 --> 00:13:03,930
Let's actually add another functionality to this application.

190
00:13:03,930 --> 00:13:09,600
Let's go to the Android studio and inside this martial art class I can override the twisting method

191
00:13:09,870 --> 00:13:12,910
that this class actually inherits from the object class.

192
00:13:13,110 --> 00:13:16,140
So let's scroll down here and here I can say two streams.

193
00:13:16,200 --> 00:13:23,190
So let's delete this value here written superlative string here because I want to put my own implementation

194
00:13:23,220 --> 00:13:24,210
of this method here.

195
00:13:24,210 --> 00:13:31,650
So now I can say return and here you can see that we have this instance variables these getters and

196
00:13:31,740 --> 00:13:32,630
setters right.

197
00:13:32,850 --> 00:13:34,760
So I can't say name price and color.

198
00:13:34,770 --> 00:13:36,520
I want to return these values.

199
00:13:36,570 --> 00:13:40,090
So first I want to return the name return.

200
00:13:40,110 --> 00:13:49,710
Get Marshlack name plus two coats for a slash in a newline character plus get martial art price plus

201
00:13:50,160 --> 00:13:55,700
two coats by Slash and plus get martial art.

202
00:13:55,740 --> 00:13:58,050
Actually color semicolon.

203
00:13:58,410 --> 00:14:06,660
So let's go to our martial art activity class and here for this Toastmasters I can't delete this value

204
00:14:06,660 --> 00:14:07,570
here.

205
00:14:07,710 --> 00:14:14,820
I can say actually when I refer to the smart object here I want to out with the string value of this

206
00:14:15,180 --> 00:14:17,610
object to the screen.

207
00:14:18,030 --> 00:14:23,790
The string method of this object is going to be called automatically right because I want to convert

208
00:14:23,850 --> 00:14:27,410
this value to a string value so the string method of this object is going to be called.

209
00:14:27,540 --> 00:14:31,950
So here I just need to say I just need to put on a concatenation operator.

210
00:14:31,950 --> 00:14:33,160
Here are two quotes.

211
00:14:33,170 --> 00:14:45,330
And here I just need to put a space so I can't say Marshall Marshall object martial art object is added

212
00:14:45,990 --> 00:14:47,150
to database.

213
00:14:47,160 --> 00:14:48,840
Let's run the application now.

214
00:14:49,650 --> 00:14:50,970
Let me show you the emulator.

215
00:14:51,000 --> 00:14:53,850
So let's click on at martial art for the martial art name.

216
00:14:53,850 --> 00:14:54,840
Just type in here.

217
00:14:54,870 --> 00:14:56,090
Boxing martial art.

218
00:14:56,100 --> 00:14:57,380
Price 2000.

219
00:14:57,810 --> 00:14:58,230
Martial Art.

220
00:14:58,230 --> 00:14:59,510
Color Blue.

221
00:15:00,030 --> 00:15:00,880
Let's go.

222
00:15:00,990 --> 00:15:01,920
So let's click on.

223
00:15:01,970 --> 00:15:04,370
At martial art now it's boxing.

224
00:15:04,410 --> 00:15:08,030
These values is added to the database.

225
00:15:08,070 --> 00:15:09,170
If I click on it again.

226
00:15:09,300 --> 00:15:12,550
Now you can see that another object with these values is added to that database.

227
00:15:12,630 --> 00:15:16,120
If I click on it again you can see that message here.

228
00:15:16,200 --> 00:15:17,110
So that's it.

229
00:15:17,120 --> 00:15:21,660
And I think that's enough for the search for you and for this activity at Marshal activity in the next

230
00:15:21,660 --> 00:15:25,290
story we're going to actually work with other activities of this application.

231
00:15:25,320 --> 00:15:30,360
So if you have any questions about the concepts that I taught you in this lesson please post the discussion

232
00:15:30,360 --> 00:15:33,490
in the course and then I do my best science and all of your questions.

233
00:15:33,630 --> 00:15:36,480
Thank you very much for watching and I'll see you in the next story.

