Skip to main content

Completing a task

Lesson 20 from: Developing Android Apps with Java

Tony Hillerson

Completing a task

Lesson 20 from: Developing Android Apps with Java

Tony Hillerson

buy this class

$00

$00
Sale Ends Soon!

starting under

$13/month*

Unlock this classplus 2200+ more >

Lesson Info

20. Completing a task

Next Lesson: Deleting tasks

Lessons

Class Trailer

Day 1

1

Overview: What is Android?

19:56
2

Eclipse and SDK setup and build a Red Flashlight app

38:05
3

Ad a button to a Green Flashlight

21:06
4

Add the Green Flashlight

18:48

Day 2

5

Week 1 - Layout and Build a Task Manager App

04:10
6

Displaying a list of tasks

40:44

Lesson Info

Completing a task

cool. So next step is we need to update a task when a task is completed. So when we click an item and, uh, change its completeness state, we want to also issue an update statement which looks like this to change the complete value of the task and up States update statement is is composed of an update keyword and then the name of the table you want update and then a list of these set statements where where you set a certain column value to the new value that you want to change it to and those are all common delimited. And then there's an optional where statement, Um, where you say, um, what sort of constraints you want to place on the update? And if you don't put that in there, that you should probably know what you're doing because it will update every row in the table to whatever you whatever you're changing. So in most cases, you want aware clause an example is I want update the task table set complete equals true where I d equals one. And if I if I skip the where the I d equals one,...

then I'll get some strange behavior. So the steps that we're gonna go through our to implement a save task method on application that updates a task in the database. And then when the task is toggled on the list item, click and view task activity, then we will call that saved task method. So let's start with implementing save task. We'll put that here under add task, and I'm gonna copy that in from my other code. Here we go. So save task will take a task, make sure it's not know useless. And then we go through the exact same set up that we did before. Um, with the content values which would basically again the key value pairs of column name to value and the other there are the differences in this case. I need to get the idea off the task, and I'm going to format my wear statement right here which aware statement looks like column name equals value. And in this case, the string is the task I d column, which is just I d. And then the value is this long. Um, I d t which I get off the tee reference to the task that we're dealing with. So I'm passing in the task. Get the I d. Construct this wear statement and then, ah, issue the update statement which takes, um, these types of arguments this look, a job, a docks real quick. Okay, so the arguments are takes this table name, content, values, and then the where clause and then where arguments? Um, actually, all just change that. So you can see how these were arguments and where clauses work together. So whereas right here I created a wear statement. So this is gonna look like this when it when it turns into sequel, Not what I Well, here's here's what mine's gonna look like I d equals, like, whatever. Five and then sequel. It will become aware statement like this. And then the whole the whole thing will be something like this. Um, update tasks set, Um, name equal new name, uh, needs a, uh, call complete equals True where I d equals five. That's that's what the update statements would look like back in sequel land. And this where Klaus I'm creating right here and just passing that in. But I could also do this. Um, change this to a question mark. drop off the I. D. Now I have aware statement that looks like this, and this is a You'll see this in a lot of different, um, places that you use sequel and any other programming language. So what I've done now is I've created a placeholder which will be replaced positional e by the arguments that I put in a ray here in this last argument to update So then the where clauses going right in here. And then I need to put a new what long array with idea in it. And it doesn't like that. What does it want instead? Oh, it once a string I see once of this strings. So then I just had empty string tie d to fake it into being a string. So now has ever understand what I did here. So now this is using the Sequels, um, interpolation or like replacement. So this task I d is going to be a question mark in here. And then these arguments will be passed to the database and then, um, put into composed into a sequel statement. So the reason this is done is to without going into too much. This is this is, um to protect against sequel injection attacks and sequel injection is where you put in. Let's say I don't I don't actually think this would work, but say I was tricky and I wanted to break the application. I could put in something like, um, drop table tasks here and then, you know, hoping that that would get put into the sequel statement and ah, actually run and this sequel back out of this This sequel says, Drop the drop, the tasks able, like get rid of all that data in the tasks table was something. As a developer, I obviously don't want to happen. Um, I don't think this is gonna happen a whole lot. Andrew adapts. It happens, But you definitely did practice protect against it on the Web. But if you have that, any situation where you gonna be putting text from a user input into your database? You'll probably want to do something more like this where you're allowing values to go through some sort of what's called a sanitization process first. Um, but in this case, there's a lot of restrictions, and the idea is never, um we don't allow the user to put in the I d ever so it's not really that big of a deal, and they're already going to get interpolated out of this content values thing anyway, So Onley in this case where I'm composing the where clause do I need to worry about it, And I probably don't need to worry about it too much in this case anyway. But that's at least out works. So, um, once we get this work and I'll have any questions about that interpolation, um, stuff with the question marks and whatnot. But so now that we have this safe task working, we can get the code working and then we'll take some questions. So what we want to do is when a ah a uh ah, see, the task is clicked. Discard those changes when a test is clicker in a call, this new safe task guy and that will happen inside of the view task activity. Suman, open that up. We'll go from the package Explorer inside a Task manager package view task activity, Um, and now on list item clicked. Ah, I call out to the adapter and I say toggle. The task completed at position that it's completed the one that was clicked, and here is a perfect time to get that task and pass it into safe task. So someone last week was asking why I wouldn't want to just toggle the the A check box inside the view instead of popping up here to the activity to do that. And here's the answer to that question. We want to do that here because the view should not be responsible for persisting the task. We want the activity to be responsible for at least figuring out where that should happen. So this is a good example of why I care about this song list item Click instead of, um, doing any certainty that work down at the view level. So import to task here. Now I have a task which I get out of the adapter by calling get item at the position that was passed in and then I call APP Save task. And with any luck, this code should run just fine. So what's the code gets deployed or the apple app gets deployed started over. Now I can say get milk is complete and if I back out of here, hit the menu applications. I'm going through this way again. Even though did dd Mrs. Faster Steak and we can see where you can do this on the on your own. APS four. Stop. I don't need to back out of this. I guess I just hold this down. Task manager, Come back here. It's booting up from scratch, and it is now complete. So now we've saved the state of the completeness in the database.

Ratings and Reviews

a Creativelive Student
 

I thought everything on this site was free and now they want 99 dollars for a course

a Creativelive Student
 

very good

Student Work

RELATED ARTICLES

RELATED ARTICLES