Skip to main content

Saving the Address on a Task

Lesson 30 from: Developing Android Apps with Java

Tony Hillerson

Saving the Address on a Task

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

30. Saving the Address on a Task

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

Saving the Address on a Task

first step is we need to complete what we started working on last week by saving an address on a task. So we've already got to the point where we know the address we want to put on a task. So now when you actually click the add task, But we want to save the relevant parts of the address on the task and, um then that task will be a location based task, a task with a location. So, uh, now we're gonna go back to and review what we did two weeks ago in the database class because we're going to have to update the database to store this new information. So now, as we go through this sort of decision tree or three steps that the sequel light Open Helper uses to decide whether it needs to upgrade the database or create the new database, we're going to be assuming that our users already have a version of the apple worshipping a new one with location awareness. And so we're gonna upgrade. We're gonna We're gonna increment the version number of the database in the sequel Light Open helper. And th...

en, in that case, the the sequel light open helper will upgrade the database and go through our call are on upgrade path. So the steps that we need to go through our to add some fields to task rule will keep a string address for display purposes and also say that in a database, and then we'll save the latitude and longitude from the address. So that's all we're going to save off of that address that we returned from the map? Um, activity last week they were gonna change the version number on the sequel that open helper, and that will set up anybody who already has the application to upgrade the database. But we also remember this is important. We need to upgrade there. We need to change and add the new columns and on create as well. This is a good form Question that somebody asked, I think just yesterday, um, how does Thea upgrade manager are the upgrade management path work when somebody is installing the database so it does not work like this? This is how it doesn't work. It doesn't go through someone who just installed the application, run on, create and then run on upgrade. It will just if the database is not their run on creates. So you always have to keep those two in sync. And this may be different from other frameworks that you've used in the past, especially rails, one that I'm familiar with. So can we ask the other poll question who has used some sort of migration management feature of a framework in the past? It's up on the screen now. Just click one of those radio buttons there and hit. Submit. Well, there's a clear lead in this one, too. And we're gonna close the poll. Three. Teoh Looks like no. OK, good. So a lot of you are just probably gonna be wondering what I'm talking about, but but I want to make this fairly clear that when you when you, uh, change anything about the database structure in the sequel Light Open helper, you need to change it, Um, for people who are newly installing application and the on create and you need to set it up for on upgrade. So the upgrade is always gonna be moving the database forward one step and on create is going to be getting it to the same point from scratch, and you need to keep both of those in sync. It always you'll run into some problems, so it's not smart enough to go through and run on, create and then run like rails is a whole set of migrations that move the database forward a couple steps from where it was before. So we're gonna look at that and code in just a little bit. And then, after all that is done in the databases in shape, then we can update the ad task of safe task in the application so that it will save the new columns off task. And so let's get into the code and upgrade the task class. Okay, my font at the right viewable size. Um, so what we want to add the task is, let's add just a string to keep track of the display address. It's called address, and then we'll add two doubles, double latitude and longitude. Okay, and that will have some little yellow squiggly lines and are here for warnings. And we can get rid of this by adding some getters and centers which we want anyway. Actually want to go through this this way instead will go to source, generate getters and centers because I will do it all at once on at address and latitude to the list. So now it'll create a get it getting said address, getting set latitude getting to that longitude. And let's say after is complete. Now hit. Okay, We've got some sweet new functions here. Teoh to access those fields. It's announced. Once we have those set up, then it will make it easy for us to Teoh change the task from outside and let the task manage those What? We also want to add a couple more helpers. I want to have a has address. Boolean method go down by address. I could find it. Here we go. So if the address is no, but I'll just say that it has no address and that'll help us later on deciding whether or not we want to display the address. Um, and then there's I'm gonna ah, at a another way of setting the address. If you just want to add a string value is the address. Um, then the field will be, uh, well, just put that directly into the address field. But if we want to set an address through a Ah, Andrew dot location that address which we do. Um, that will make it easy for us to add an address. Um, and then you could I said it to know, said the latitude and longitude to know. But if if it is not an old and we will get the, um well, just for the purposes of this application, you could get a little bit more fancy with this for for what I'm doing. I'm just, um, adding a string buffer here and then iterating through all the address lines and just Kincannon ating those together into one line. So then all the address lines will just be on one line, and that'll be good enough to show pretty much of all that. We need to know about the address for our purposes, and then we'll also get the latitude and longitude off of the address. And, ah, set that on the field. So now we have two different ways to set an address on a task. And then finally, I'll add another little convenience method here to say whether or not we have a location, because that will help us later when we filter tasked by location and that will just be checking to see if Latino lunch soldiers are the default values of zero. Okay, now we've got task in shape. Let's open up in the source tree. The activity where we add tasks does the ad task activity. And we've got this address field here which is set up, um, to be the address that comes in from the map screen that we worked on last week. So if that addresses set, then the next thing we want to do when the user actually ah saves the task or as the task that's in the ad task method here of so the way we do it now is we have the application that we had the task, but we also know want to set the address on the task. Said Address will set that to address which is the field on this activity. And then we'll change at tasks that it knows about addresses and saves those to the database. Um, you know, in another couple steps, First of all, before we do that, we want to update the sequel Light Open helper. So when I open up the secret open Helper Task sequel, I'd open helper, hoping in just through the open resource panel in eclipse. All right, so what do we want to do here? There's a couple things we want to add. A couple mawr constants, which will stand in for the names of the columns we want to add to the database. I don't have an address column, which will just store string latitude column and a Longitude column. And, um, the next step is to upgrade the version. So now we'll just set the version number two, too. So if anybody was already anybody already had this application installed sequelae open helper would know that the current version is one. And if it was one, then it will now see that it's to when we pass that into this super constructor here online 20 and ah, it will know to run the on upgrade path through ah through this secret open harbor. Now, if no, if they didn't have it installed before then, they would run on create. But we have to upgrade both of those. So next step is on upgrade. Here's what we want to do. Um, we already talked about this a little bit in the homework for the database class. Ah, we want to alter the table. And the statement looks like this altar table table name at column. In this case, we want to add a column. And then here's the name of the column. And then here's the type. So we want to add a text column for storing the address. Open ended your column for storing the latitude and longitude because those should be big enough to store that the location to the resolution. That's good enough for us on and will make sure of that inside the application in a little bit. So now the on upgrade path will work the way that we expect we're gonna have, um we're gonna have those three columns added to whatever there was there before. But, um, what we need to do also is make sure that the create table has the correct calling information as well. So underneath here and test complete line 41. I'm gonna add the other calm that we just added here. Um, now we have I d named completes on we've added address latitude and longitude. But ah, make sure all this this is kind of hard to read and parts. You have to make sure that there is a comma because this is going to get constructed into a string of texts sent to the database management system. And, ah, about that comma things, they're not gonna work. Work well, so you have to. Maybe it might help you to To write out the sequel statement in text first and then go in and add this of this stuff. Um, uh, these constants to make sure that that we're using the constant names and the names that we expect to be using all over the application. Okay, so now the database is going to get up created and or in the case where we're just a song APP knew the database will be fully created the way that we want. Um, there's one last step inside the application when we add or save a task, we need to make sure that we're saving the latitude, longitude and address as well as the the other columns that we save off the task. Open up the application file test manager application again, show you inside the code. And com O Reilly, Andrey, task manager task manager application. Um, and here's a nice little way to Ah, navigate around a file if you want, um, to open, um, a particular method I want ad task groups have done a wrong thing here. I think it's actually control. Oh, yeah. So I think that's control. Oh, on Windows Commando on Mac. This shows you all the methods in the file, which is pretty nice so that you could just start typing add task, and then it will filter the list of all the methods to to be the one that matches the pattern that she just typed in. So now here we are in ad task, and the remember content values is like the sort of hash like object that we used to to specify columns and values that go into columns in the data base in search statement, um, for, for instance. So we need to do here in ad task is at a couple more key values Paris to the content values guy. And now we have the name complete address, latitude and longitude off of the task that will be inserting into the database. And that's on an ad task. So that's a database insert. And then it saved task When we do a database update, we need to do the same thing. So just copy and paste those in and think should works just fine. Okay, so that's what we wanted to do for that step. Let's just do a quick smoke test to make sure nothing blows up. And once this starts over again and we could go out a task with the location, uh, use our standard amphitheater way Mountain View, California and I should find our location. I'll look. We have started the code for last week with Shadow Way Offset makes it look kind of cool all this. I think this is the right location not like right here, which is a little confusing. So that's maybe one reason to not move the shadow. But, hey, looks good enough to not matter for these purposes, right? So sad location. Some 1600 Amphitheatre Parkway should be a that location that's added to the task. Let's say we'll visit Google. Then we add the task. It should not blow up. Good doesn't blow up, and now we have a task. So the next step will be to add the at a location line underneath any task that have a location inside this view. So let's see first, if we have any questions about the steps that we just went through. Yes, I have a couple for you. One person asked if we need if else statements in the on upgrade method to handle multiple upgrade paths. So if somebody had one and then they're upgrading to three later, maybe. And don't go through two. Or you know how you handle once you've got a couple of versions out there? Yeah, that's a good question. So, um, yes, you probably will, because not everybody's gonna be upgrading from the same path. Yeah. So, um, you might want to do, like, a switch case on the on the I think if you use a switch case, you may be able to do something tricky with falling through. Um, if you understand what I mean, Um, it's the case statement to do, like, you know, if your I think you do it sort of in reverse order. So if it was three, just add this column. But then three will fall through two to maybe maybe that wouldn't quite work. I don't know, But in any case, Yes, you will need to check the version number. And at don't Onley add the columns that you need. Then that's even more advanced situation. And then the other question I have for you is a couple people noted that, um, you're using Inter juror for last long. And I remember last week we we didn't We shift the values and convert. Hm. That was just for the display. We didn't actually change. Um, see, so is in a map. Task Yes, or the map activity. Now, that was inside the overlay. Ah. You put that views address overlay. Yeah, we just We didn't actually save that. We just moved that to a manager for the Geo point on the address overlay. Is that what you mean? Well, so I guess if if that's not the value that's actually being saved in the database, uh, I guess people were noting that interference that afloat for the database. Oh, yeah, right. Um, you know what? That's a good point. I think that was probably an oversight. And the example code. I probably need to convert that, um what we do that just just Well, just see the same thing we've got right here. That's a good point. Um, see, Sequel. Wide open helper. Or maybe I could just do is afloat, too. Let's start as a float. It's not a gloat. I'm not sure why I set that up his image. Or maybe I expected to do the conversion and then just didn't end up doing it. But in any case, it's a good catch. Ah, see, you know we haven't got to yet is where we actually pull the pull the information out. Yeah, I'm looking at my sample code. Anybody that's looking at the sample code that I put up last night, we'll notice that I pulled it out as a float. So that's kind of strange. I think that was just an oversight on my part. Inside inside the sequel Late Um, open helper. So those should be floats. I'll change that in the source code and then update Thea The example. Code Tony. Now you need to upgrade to version three of the database, and the person's question of on upgrade needs to have the a statement in it, since you just change the type that I just did that Yeah, that's a good point to, um let just do this just for this just right now. Keep it simple. Yep. This is the benefit of having a live audience that can pick up when I make little mistakes like that. And we can do some on job training for the instructor. Okay? So just if the versions three in this case will upgraded to go Got to get rid of this one. Ah, you know what? I would have to change the column. I'm not sure how to change that. That Ah, I don't know. The Sikh. I don't know. I don't know the syntax for changing a column. Ah, What I'm gonna do instead is just like this and cheat. Oh, I didn't run the 2.1 version of the emulator. It's now filter through. All these guys should at least giving you a filter here. You're gonna delete the app. Yeah, I'm gonna delete the up. I was gonna explain that. So I'm deleting the app. So now there will be no database to worry about. And you could see why upgrading the migration stuff is pretty tricky. You have to make sure that you think through what you want to do. So now discover it of this because we're just gonna assume that we've gone through only one version. Just a Nobody's hopefully confused. Hopefully, nobody's confused. Okay, so now create table's gonna get called, And then latitude and longitude will be floats as we expected. And I'm just gonna run those guys in the background. Okay? So any other questions didn't make any mistakes during this? Somebody pointed out that address returns to double is afloat in ah, SQL light able to hold it double. Yeah, that's a good question to I think that it is because there's, um I don't know. Let's just go look that up. So I'm gonna open up Firefox Secret light. Um, there tapes. She'd get me there. It's not. It's not float. It's riel. Oh, goodness. I think I think is that they, uh I wonder if it if it will take float anyway. Maybe it's better just to go back to energy after all this. Yeah, we'll lose. We'll lose some resolution that all changes to real. And then if there's any other questions, we can answer those well and do this real briefly. Tony, Um how do we came? Beautifully. How do we change the name of a table in sequel light. Um, there would be another altar table. I think you're able to alter the table without that. Also, the table name without dropping it, Um, you'd have to read the documentation. I'm not I don't know offhand. If you can alter the table name and change, change, change name or something. I don't know what it would be. What if you look that up? If you otherwise couldn't do that, um um, if you can't change the name in other words, then you'll have to get all the data out of the database and store it somehow in Ah, job. Ah, structure. Drop the database and then re create the database and then add all of the data back into the database. So that be kind of Ah, maybe kind of a lengthy upgrade process, but that's what you need to do. But I think during that you can do something like, um, you know what once were What's we're gonna, um we're gonna look into this in the next glass, uh, running tasks in the background. You'd want to run something like that in the background, and then so the user update Progress. Um, like, here's here's what we're doing right now we're upgrading the database, and then, uh, it's gonna take a little bit of time. So here's installer bar like that or something. And then once it's done, um, back to their regularly scheduled service. But yeah, you'd have Teoh, you'd have to do something like that. Hopefully, that makes sense.

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