Skip to main content

Deleting tasks

Lesson 21 from: Developing Android Apps with Java

Tony Hillerson

Deleting tasks

Lesson 21 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

21. 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

Deleting tasks

So the next step is when you delete tests. We want to get rid of them out of the database. So now this is down to their last in our crowd document of the D part of our crowd. Acronym. Create, read, update, lead. We've done all of those things except elite now, and we want to delete the tasks out of the database when the user clicks to remove completed tasks and there are completed tasks. Um, in the lists, we will delete them, and that is done by issuing a delete statement. And the lead statement is simple. You say delete from table, where some criteria is true and almost even mawr than update statements. You better have aware statement into delete statement are aware clause in a delete statement. Because if you don't, although the where clauses is optional than the delete statement will delete everything, which is probably not what you want. So here's an example delete from tasks where I d equals one. So what we need to do is, since we're gonna be potentially deleting whole list of tas...

ks, any of the ones that are complete, we need to come up with a way um to get all the ideas of the delete of the tasks that we deleted from the list when we click this remove complete task button and then on on the application will implement a method that takes a list of ideas and delete all those tasks that match those ideas because we're going to delete a group of them at once. So let's do that. Ah, we will first of all, implement that method on the application. Somebody go to the task manager application after save task. I'm gonna paste in some code here which will do are deleting for us. And the way that I'm going to do that is, instead of issuing a list of delete statements, which is something I could possibly dio, I'm composing them into one delete statement, which deletes from Basically, here's what's going to be like where I ds in. This is a sequel thing in 157 whatever. It started with Collins 1 40 for 25 whatever. Some some arbitrary list of ideas goes here. And this will delete um from the task table where the I. D. S are in this list. And that's what I'm composing up here, that's that's the end result, or that's the That's the goal that we're trying to get to. So we're gonna take a list of Longs as I DS and then make a string buffer and then just pop these guys onto that string. Buffer, separated by a common unless we're at the end of the ideas, is pretty standard stuff so that we don't end up with a hanging comma here, which will blow up. So that's pretty simple. Um, And then I composed the statement here by creating the where clause here where, um, something in this list, just like I did before and then past that into the elite statement on the database. Which takes table is an argument. A where clause. And then let's hear this looking help really quick because there's one more. Oh, yes, where are eggs? So this is just like before any of those we could pass in, um, an array instead. Um, it's just that I'm not sure that I could do the interpellation with list in that case. So I'm just going with this. I'm not going. Teoh tryto try to test that out. What I mean is, I could Probably I might be able to do something like this, but I don't think I can. That's that's We could look into it later if you could or not, but, ah, they're replacing that question mark later on with an array of I ds. You know, something like that. Um but I'm just gonna stick with this because I'm not worried about any sort of sequel injection in this case, so it's not really a big deal. Okay, so now we need to call this and the right place to call. This is inside the view task. Active activity after remove completed tasks. Um, is called so inside the view task activity. Ah, when remove completed task is called, we know that we're gonna remove them from the adapter, and then we need to somehow call up to the application and say, delete tasks, which we just created in passing a list of all the I ds. So where we going to get those? I ds. What I've chosen to do is to do this make a specification, um, that they removed completed task change so that we're on. We're beyond re factoring now. We're actually changing a method signature So we're changing some AP eyes, but everything else is pretty clean, right? We just kind of kept changing things in the application and other places didn't really need to know too much about what was going on besides implementing some new functionality which wasn't there before, Um, a lot of cases we were doing some real nice cleaner factoring, but now we actually have to change this method signature on remove completed tasks to return a, uh, and A list, an array of longs. And here's what we'll do there. Let me open up the code and just pop it in there. After notified data set changed which will update our list, we're going to return completed, I ds um which I'm going to create as a new local variable completed ideas. A realist task which I want to be a new or a list blank and then whenever a completed task is is added to this to this other ray which we, uh which we used to to do this tasks that remove all little step down here and the dance We're also going Teoh add task that I d to completed I ds and I think I need to do a cast here. Uh, I thought auto boxing would have taken over taking over for me, but I guess not. Look into this real quick. It's the somebody saying that Ray list might be wrong. Really? So, yeah, I don't know that happened. There we go. Nice work, Eclipse. Thanks for nothing. Sometimes you have to watch after the generated code. Okay? So, yeah. So auto boxing takes over in the long, will turn into a long object and will pass around the long array to whoever calls us once we remove the completed tasks. And that should happen once we click the removal tasks prove completed. Task button here, and then we'll call up to deleted task. So let's watch that inaction and then we'll be ready for some questions with time to spare. It'll be nice to have a little bit of time at the end. A little bit of breathing room. Okay, So get milk only. Just add one. That's not, um, at task. Now we've got one that's completed. One that's not remove. Completed task does not blow up, which makes the instructor happy. And now I'm just gonna go to Dede. Messed killed us so that we can prove it. Comma. Riley Andrey, Task manager. Stop! Switch back to the emulator. Hold down the home key. Go to task manager. And there it's proved. So now we're deleting tests out of the database. Okay, so that's all the steps that we have to to biting off what we decided to chew up today, which was to crowd tasks from a local secret light database. Um, are there any questions about the deletion feature? Yes. Well, I actually had one question that a ton of people asked related to deletion, which was Why not just delete from tasks where completed equals true rather than having to pass the list of I ds, um, you know, I could have, um I've left this. I think when I implemented I don't know. I don't have a good answer that, but I'm trying to think back to what I did when I created this code. I think what I wanted was a more general, um, completion where I can complete where I'm sorry, um, or general delish in where I could pick and choose which ones to delete, um, generically. Instead. But there's yeah, in this the way it turned out to be. I think I might have started writing the code or coming up with when I wanted to write before I really centered on what the feature was gonna be. Um, so, yeah, there's no reason that that could be done that way. But this way, I've got something that will allow me to remove a list of ideas no matter what I'm. So I could like I could do a delete from, like, a detail view or something. Or you could remove a test that wasn't complete. So yeah, um I mean, like, I could also do. I mean, I could also implement methods appear delete tasks are duly completed, task or something for a special purpose to lead all that are completed. So they add, It's totally valid. I would going back, actually probably have done it that way. But I think I started ready before I even knew what I wanted the tap to be at this point

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