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
€13.99
List Price:  €65.30
You save:  €51.31
£11.97
List Price:  £55.85
You save:  £43.88
CA$20.47
List Price:  CA$95.55
You save:  CA$75.07
A$22.85
List Price:  A$106.62
You save:  A$83.77
S$20.39
List Price:  S$95.18
You save:  S$74.78
HK$117.40
List Price:  HK$547.80
You save:  HK$430.40
CHF 13.67
List Price:  CHF 63.79
You save:  CHF 50.12
NOK kr164.96
List Price:  NOK kr769.72
You save:  NOK kr604.76
DKK kr104.38
List Price:  DKK kr487.07
You save:  DKK kr382.68
NZ$25.10
List Price:  NZ$117.16
You save:  NZ$92.05
د.إ55.09
List Price:  د.إ257.06
You save:  د.إ201.97
৳1,648.26
List Price:  ৳7,690.80
You save:  ৳6,042.54
₹1,251.95
List Price:  ₹5,841.61
You save:  ₹4,589.65
RM71.54
List Price:  RM333.81
You save:  RM262.27
₦19,876.95
List Price:  ₦92,745.84
You save:  ₦72,868.89
₨4,181.05
List Price:  ₨19,508.81
You save:  ₨15,327.75
฿555.22
List Price:  ฿2,590.69
You save:  ฿2,035.46
₺485.45
List Price:  ₺2,265.11
You save:  ₺1,779.66
B$76.75
List Price:  B$358.13
You save:  B$281.38
R281.14
List Price:  R1,311.83
You save:  R1,030.68
Лв27.37
List Price:  Лв127.74
You save:  Лв100.36
₩20,652.15
List Price:  ₩96,362.95
You save:  ₩75,710.79
₪56.45
List Price:  ₪263.40
You save:  ₪206.95
₱865.52
List Price:  ₱4,038.52
You save:  ₱3,173
¥2,338.33
List Price:  ¥10,910.67
You save:  ¥8,572.33
MX$256.91
List Price:  MX$1,198.75
You save:  MX$941.83
QR54.76
List Price:  QR255.51
You save:  QR200.75
P206.72
List Price:  P964.58
You save:  P757.85
KSh2,025
List Price:  KSh9,448.65
You save:  KSh7,423.65
E£717.75
List Price:  E£3,349.02
You save:  E£2,631.27
ብር861.92
List Price:  ብር4,021.76
You save:  ብር3,159.83
Kz12,510.90
List Price:  Kz58,375.85
You save:  Kz45,864.95
CLP$14,262.15
List Price:  CLP$66,547.19
You save:  CLP$52,285.04
CN¥108.63
List Price:  CN¥506.87
You save:  CN¥398.24
RD$880.44
List Price:  RD$4,108.14
You save:  RD$3,227.70
DA2,016.43
List Price:  DA9,408.68
You save:  DA7,392.25
FJ$33.83
List Price:  FJ$157.85
You save:  FJ$124.02
Q116.80
List Price:  Q545.03
You save:  Q428.22
GY$3,141.98
List Price:  GY$14,660.48
You save:  GY$11,518.49
ISK kr2,103.60
List Price:  ISK kr9,815.39
You save:  ISK kr7,711.79
DH151.70
List Price:  DH707.84
You save:  DH556.14
L265.48
List Price:  L1,238.76
You save:  L973.27
ден861.69
List Price:  ден4,020.67
You save:  ден3,158.97
MOP$121.10
List Price:  MOP$565.05
You save:  MOP$443.95
N$283.35
List Price:  N$1,322.15
You save:  N$1,038.79
C$552.70
List Price:  C$2,578.94
You save:  C$2,026.23
रु2,002.28
List Price:  रु9,342.64
You save:  रु7,340.36
S/56.41
List Price:  S/263.24
You save:  S/206.82
K57.97
List Price:  K270.50
You save:  K212.52
SAR56.25
List Price:  SAR262.50
You save:  SAR206.24
ZK397.98
List Price:  ZK1,856.98
You save:  ZK1,459
L69.63
List Price:  L324.92
You save:  L255.29
Kč352.27
List Price:  Kč1,643.69
You save:  Kč1,291.42
Ft5,486.21
List Price:  Ft25,598.69
You save:  Ft20,112.47
SEK kr163.77
List Price:  SEK kr764.15
You save:  SEK kr600.38
ARS$13,121.32
List Price:  ARS$61,224.11
You save:  ARS$48,102.78
Bs104.15
List Price:  Bs485.98
You save:  Bs381.82
COP$59,389.48
List Price:  COP$277,111.33
You save:  COP$217,721.85
₡7,631.24
List Price:  ₡35,607.40
You save:  ₡27,976.15
L370.86
List Price:  L1,730.46
You save:  L1,359.59
₲111,824.77
List Price:  ₲521,774.38
You save:  ₲409,949.61
$U579.58
List Price:  $U2,704.33
You save:  $U2,124.75
zł60.52
List Price:  zł282.40
You save:  zł221.88
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.