CRUD Activity - (b) Write Code

17 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$15
List Price:  $69.99
You save:  $54.99
€14.39
List Price:  €67.18
You save:  €52.78
£11.97
List Price:  £55.85
You save:  £43.88
CA$21.04
List Price:  CA$98.21
You save:  CA$77.16
A$23.06
List Price:  A$107.62
You save:  A$84.56
S$20.19
List Price:  S$94.24
You save:  S$74.04
HK$116.76
List Price:  HK$544.83
You save:  HK$428.06
CHF 13.40
List Price:  CHF 62.54
You save:  CHF 49.13
NOK kr166.08
List Price:  NOK kr774.97
You save:  NOK kr608.88
DKK kr107.37
List Price:  DKK kr501
You save:  DKK kr393.63
NZ$25.70
List Price:  NZ$119.95
You save:  NZ$94.24
د.إ55.09
List Price:  د.إ257.07
You save:  د.إ201.97
৳1,792.02
List Price:  ৳8,361.58
You save:  ৳6,569.56
₹1,266.57
List Price:  ₹5,909.84
You save:  ₹4,643.26
RM67.02
List Price:  RM312.71
You save:  RM245.69
₦25,393.24
List Price:  ₦118,484.88
You save:  ₦93,091.64
₨4,167
List Price:  ₨19,443.22
You save:  ₨15,276.22
฿516.99
List Price:  ฿2,412.28
You save:  ฿1,895.29
₺518.28
List Price:  ₺2,418.32
You save:  ₺1,900.04
B$87.01
List Price:  B$406.01
You save:  B$318.99
R272.33
List Price:  R1,270.71
You save:  R998.37
Лв28.15
List Price:  Лв131.35
You save:  Лв103.20
₩21,067.65
List Price:  ₩98,301.65
You save:  ₩77,234
₪55.72
List Price:  ₪260.01
You save:  ₪204.29
₱884.08
List Price:  ₱4,125.14
You save:  ₱3,241.05
¥2,321.55
List Price:  ¥10,832.35
You save:  ¥8,510.80
MX$306.42
List Price:  MX$1,429.79
You save:  MX$1,123.36
QR54.69
List Price:  QR255.18
You save:  QR200.49
P205.08
List Price:  P956.90
You save:  P751.82
KSh1,942.03
List Price:  KSh9,061.51
You save:  KSh7,119.48
E£744.75
List Price:  E£3,475.01
You save:  E£2,730.26
ብር1,835.83
List Price:  ብር8,566
You save:  ብር6,730.17
Kz13,695
List Price:  Kz63,900.87
You save:  Kz50,205.87
CLP$14,668.88
List Price:  CLP$68,444.99
You save:  CLP$53,776.11
CN¥108.64
List Price:  CN¥506.93
You save:  CN¥398.29
RD$903.78
List Price:  RD$4,217.06
You save:  RD$3,313.27
DA2,014.12
List Price:  DA9,397.90
You save:  DA7,383.78
FJ$34.13
List Price:  FJ$159.29
You save:  FJ$125.15
Q115.75
List Price:  Q540.13
You save:  Q424.37
GY$3,137.33
List Price:  GY$14,638.82
You save:  GY$11,501.48
ISK kr2,094.62
List Price:  ISK kr9,773.54
You save:  ISK kr7,678.91
DH150.86
List Price:  DH703.91
You save:  DH553.05
L273.32
List Price:  L1,275.33
You save:  L1,002
ден885.71
List Price:  ден4,132.76
You save:  ден3,247.04
MOP$120.23
List Price:  MOP$561.01
You save:  MOP$440.78
N$271.95
List Price:  N$1,268.91
You save:  N$996.96
C$551.80
List Price:  C$2,574.70
You save:  C$2,022.90
रु2,025.24
List Price:  रु9,449.77
You save:  रु7,424.53
S/56.86
List Price:  S/265.32
You save:  S/208.46
K60.37
List Price:  K281.71
You save:  K221.34
SAR56.31
List Price:  SAR262.77
You save:  SAR206.45
ZK414.68
List Price:  ZK1,934.92
You save:  ZK1,520.23
L71.67
List Price:  L334.42
You save:  L262.75
Kč364.89
List Price:  Kč1,702.59
You save:  Kč1,337.69
Ft5,925
List Price:  Ft27,646.05
You save:  Ft21,721.05
SEK kr165.54
List Price:  SEK kr772.42
You save:  SEK kr606.88
ARS$15,056.02
List Price:  ARS$70,251.41
You save:  ARS$55,195.38
Bs103.62
List Price:  Bs483.50
You save:  Bs379.88
COP$65,846.24
List Price:  COP$307,238.59
You save:  COP$241,392.34
₡7,638.55
List Price:  ₡35,641.51
You save:  ₡28,002.95
L378.96
List Price:  L1,768.23
You save:  L1,389.27
₲117,070.05
List Price:  ₲546,248.87
You save:  ₲429,178.82
$U639.33
List Price:  $U2,983.14
You save:  $U2,343.81
zł62.45
List Price:  zł291.40
You save:  zł228.95
Already have an account? Log In

Transcript

Okay, so next we come over to the crud activity, as the name suggests this activity which is going to allow us to perform some of the CRUD operations, like inserting a New Scientist, updating an existing one. And then deleting us to the listing. The title, of course, is being performed in our database activity. Okay? So crud is going to be responsible for adding a new scientist, updating an existing one, and then of course, deleting or some existing scientist. So we can reach out straight away, we're going to jump into our inputs.

So start by putting a context. Then, of course, our date, time picker edittext. Then we're going to put our bundle object we're going to input our menu as well as the menu inflator. Sorry, menu item, then of course our edit text. Then our text field Then our app compat activity. Now we're going to continue input, of course, under inputs.

Then public class credit activity extends app compat activity. So of course, you can see we've already added or inputs. Now we've created this class, which is deriving from our app compat activity. Of course, the next thing is to define, of course, our instance pills, which majority of them are going to be our widgets. So come and say we're going to have, for instance fears, private edittext type of text, a description text, a categorical claim of data, then private text to edit. Then private dictum pickup in the text, the tipsy and then private for context, equal to productivity do this, then very few mugger that will do more good Then dairy received dairy in private thinner string array, tanks equal to morning, afternoon, evening.

So we'll come on to initialize them privately initialized widgets, heretics equal to find view by ID, app ID, indicator NXT. Data txt, our ID the title txt, then description txt, our router ID, description txt. So we continue and initialize all digits. Then of course, we set our date format that form the same except for mud utils that deck format. So we'll come and say private void, listen to any texts, clicks, comments, a category txt, the set onclicklistener then utils. Delegate, crud activity to dismiss true, of course category C, then time of day, the tech Set onclicklistener you choose the set delegate credit activity with this, then first, then of course time of day.

So, of course, what we're doing right here is that we're listening to the click events for this to detect. Then we'll click to using a lambda expression feature selecting to invoke or select delegates. So this, select the Select delegate, which is a method, which we defined in our utils class. Basically, it allows us to select, of course show a single cheese dialogue with these are going to select an item and then that will be tempted to select a group be set today, it detects that to pass feature. Okay, that is the same thing with this thing. Right here.

Okay. Yeah, let's continue. So we say that the main goal of this activity is to allow us to perform our CRUD operations. Thank you. Clicking on new scientists or inserting a New Scientist updating as well as deleting, let's start with one method which is going to allow us to actually insert Of course, in such we're going to make use of the methods which weren't defined enough. Of course, we will create a class okay.

So let's go ahead continue and create this method. So, private void insert dairy, then string title, string description, then string category and the string and the string time of day, then you come and say dairy, dairy, equal to new dairy, dairy, the set ID string to work off system current time. Then there is except title, title, the description, description and the dairy the set category category, then during that set that date, then there are three separate of date time of day and then dairy with set category times the new random nextint things different then result equal to dairy. Dairy, if result is cooler than result is greater than negative one then crushing manager there is not equal to true it is that clear in the text title texting description texting and ditch texting, then utils which show this then such successful he tells the show this then in such unsuccessful airs it gives the show this then, and successful arrow, okay so, this is three tier error occurred as a consequence with design method which allow us to receive inputs.

Okay? If you save, of course parameter inputs parameters, and then of course instantiate a new object, of course, you're setting our ID explicitly. And we're going to use the current prime minister obtained the current time in milliseconds, and then turn to a string and then use it as our primary key, which is ID, then you can to set of course our title description category as for the category, we're going to use a handle category from the three the two already defined. Okay, morning, afternoon, evening, okay. So we set them in our category, then of course to action in such we need to invoke the such that are defined in our filmography, this came to the result, the new check its contents, okay, if it's not equal to null Yeah, we proceeded to present the negative one then you know that you've been successful, though is we haven't okay.

So this is this how to insert data. Okay, so the next thing we're going to do is structurally make use of this method. Okay? So, to make use of it, let's define another method. So predictable insert data in common is true Kato description as well as that as well as category as well as time of day then if you choose the fully lit title description, then will come and say title equal to title 60 get text to string at the description equal to description take see that get text to string, then of course, the same thing with the category and the same thing with our time of day. Then if that exceeded that is much cleaner than to come and say that equal to the text Much look for much like to see that get that then else they take see that set error invalid.

Then they teach you to cost focus. Then in such dairy, we invoke the method, pass the title description category, the container D, and the zip. Okay, so we've actually made, as you can see, in this second method, we're making some evaluation criteria. And then of course, you came to us from our user interface, and then actually passed them to our insert data service. Proceed. So, of course, more or less, our update method is going to be almost similar to this one, except that you're not going to instantiate a new dairy in dairy, okay, that is the dairy that was the data that was passed to this particular activity, so that you can update so Get Data, protect my code.

This is how we interpret our data, you can see we're receiving these parameters, then of course, we set them to a receive value. And then you invoke the update method defined enough room or group will check in, of course, we checked. If successful, the new group show up, it's successful. We should, of course, the era that occurred, okay, we should define it as actually being successful. So this is the method for loss to date. Of course, just like with the insert data, of course, the method mechanism is very, very simple.

We're going to start by performing for dictation and then invoking this. So here we go. Of course with our update diary, you can see first of course, pay to this distribution, they can category then of course, we perform our validation Then of course, obtain our data from our widgets, then of course, pass them over to our dairy okay. So, this is these are the two methods that are coming toward us to update our data this move over into our delete previous private for delete data, then integer result equal to zero delete the data, then of course, if result is not equal to null then if result is greater than zero then cache manager is nothing equal to the new tools which show this then delete successful then of course, it is the show this then delete and successful errors utility utils the chop this then a successful error occurred Okay, so this is how we're going to delete our literature.

Okay, as you can see, we just invoke the Delete method at different enough room and then show our message. Okay, so this is it. This is our delete data. Let's proceed. So the next thing we're going to do of course is to hold on back pressed. So if there's a press the back button, we're going to share this info dialog polling data if he or she actually wants to exit.

So next thing we're going to come to our on create Options menu return some of the blooper reading, then let's add the following code. So if received are equal to null that would get me in photography and fit our menu returning item menu menu that takes you to set text at New direct advice. Get menu inflator deconflict menu, the added item menu menu, then hit the tick see the set text in existing data. Okay, so what have we done later? Well, we're checking for cedar equal to null. And of course, we know that this particular activity was opened for the sake of adding a new item.

And in that case, we're going to insert the new item menu as our menu, click OK, then this information that should not show up. Now, if the receiver is much cleaner, then we know that these pages open for the sake of editing. Hence, we're going to inflate the Edit item menu. And then of course, shut this particular message as our traitor. Okay, so this is our own Options menu. Let's go ahead and read our next callback.

So that next callback is our own options. Item selected is going to be Based on the user selects a single menu item. So let's listen to this menu, item selection fence. So we'll come and say switch item to get item ID then come on stickers are detected Insert menu item, then in such data other ways, return true, of course of inserting our protected Edit menu item, then if it isn't called now, then come and say, update data. Otherwise, it tells the show, of course, or the editor only works in editing mode. Then return true.

Case, Id delete menu item, then, if received every call to null, then delete data. And there is deleted, of course editing mode. Then we'll return true. Case advertising to reveal our menu item, commentators that open activity this then the activity class, then of course we finished the current activity, then return true. I've attributed it to Tom. Never get up for some task, then finish the current activity, then return true.

Okay, so this is a tweeter. This is how we're handling on options. Item selected, if these are selects our options menu, these are what we're going to do right here. Okay. Yeah. So go ahead override this particular method.

Now this method Google for elite So that you're able to inject, of course, our custom fonts. So within this activity recap, and we'll be using, of course, elliptic curve cryptography. So, of course, when activity is resumed, we're going to receive our data, then set them to their editing widgets, some comments say, October rate, then protected void on resume, then come and say so public on resume, then very courteous depressive, very good intent, they'll see then if oil is not equal to null then received a very equal and then title text, set text is in the title, then description text in the text received very good description received very good category received very little time of day, then object that equal to this See that we did get that certificate it is not equal to null, then that takes into that utils that give me that and that the string abilities.

Okay. So this is our own resume, of course, you can see what you're doing Rita. Basically we're receiving our data and then assigning to receive data. And then of course, we get the title description category thing to take into their respective widgets. Okay, so we're going to be dism of course, in our own tourism and activities actually resume. So of course, we're going to write our own credit, then our onClick method.

Let's come in and see it for it. Then predicted vote on credit vendor, second sunset, and of course, we come up with a super little clip, we'll pass this Install set, then set content view our activity crab, then we will model equal to the properties of this get. Then the review mode group which contains the initial is widgets, then this isn't an ad text clicks, and does it Okay, does our on click method feature. So case does it does a chrome activity. For important closes, you're going to perform our inserts as a result deletes Okay, so here we go. Credit activity.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.