Skip to main content

Showing the Tasks

Lesson 13 from: Developing Android Apps with Java

Tony Hillerson

Showing the Tasks

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

13. Showing the Tasks

Next Lesson: Completing 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

Showing the Tasks

So next step is to get the tasks into you the list. What we're going to do to make that happen is to create a custom view that will show, um, each one of the task list items. So each element in the list and we're gonna make a list adapter, Um, which is gonna be the data provider for the list for you. So let's talk about what an adapter does. An adapter is the ah. It's the instance of the class that is, um, responsible for taking data out of a list and telling the list view that needs to display that data, Um, of the answers to a few interesting questions and also performing for you important tasks. So the list view needs to know how many items that are on the list. And so to figure that out, it asks the adapter. The list view sometimes needs to know. Um, what is in it needs to uniquely identify each item in the list. So the list adapter is also responsible for for helping that list. Do you do that by, um by providing an A p I tiu ah, to get the I d for each specific item in the list, a...

nd sometimes that can. That can be an I D that you define. Or that could be an I. D. That is just based on position or something like that. But the list adapter is responsible for uniquely identifying each item in the list. Um, it's also were responsible for getting the data for an item at a position and for providing the list of you with the view that needs to be displayed for each item that the list view asks for. So it's responsible for a lot, and, ah, it allows the data to not have to know about any of that stuff. And it allows the list view to not have to worry about those kind of details. It delegates those all off to the list adapter. So the list adapter is a pretty important part of this whole, um, displaying data in a list. So the steps that we need to go through to accomplish the tasks that we have for this step is we need to create the list adapter. Ah, we need to implement some of the interface that that defines, and then we need to create a custom view that will be responsible for displaying a single task. And then we need to return an instance of that view from the adapter on the give you call, which the listed after is gonna call her. I'm sorry, the list activity is gonna call, So let's start out by creating the adapter. So I'm going to come here to my package explorer, and I'm going to create a new class by right clicking this project. I'm sorry, this Ah ah, this package inside of the package Explorer and go to new and go to class creating new class. And I want to put this adapter in its own package so I can find that easily. The name of this is going to be just task list adapter and it's super class is going to be base adapter. So that is enough to complete this. So the base adapter class is provided by Android as an implementation of the adapter interface. And ah, it's a very basic adapter that just sort of provides implementation of some some things underneath it. But there's also some things that we need to implement as well. So you can see that there is ah um, an error here in eclipse, I can hover over this base adapter class and say, Ah, basin after cannot be resolved to type. That's where it didn't find it. Any case that, um, to pay? Ah, Thea Ah, Organize imports. Anyway, that the package is for that is android dot widget dot basic after there? No, she able to find that now, since we've imported that, um, the task list adapter says that it has some problems because it's not implementing all the methods that air abstract inside base adapter. So I'm gonna come here and hover over the task list adapter class and se um, at unimplemented methods, which is nice. You have to go figure out what those are our and then a whole bunch of code gets put into my file. So here are the methods that got added by based adapter or by implementing all the abstract methods of base adapter. Get count. Ah, get item. Get out of my D and get view. So those are those are the ways that that list adapter fulfills its responsibility. Um Ah, that that the task list, um, list of you is going toe delegate to the adapter. So what we want to do? So let's go look at where we're storing, um, the list items right now, which is inside of the task manager application, which is again, application follow kind of present throughout the whole application, no matter what act, what activities being shown. So the task manager application, um, holds a list of the current task, which is just an array list of the type task. Ah, What I think we want to do inside the task list adapter is do the same thing. We're gonna have it have a reference to that list as well. So let's have its hold a private variable of type of ray list and need import that guy. He thinks there's a syntax. Ah, I'll just keep on typing until I could get here and get eclipse to Teoh. Fix this for me. So I'm just gonna have a list of tasks. Honore List of tasks hover over this important Java Util array list and we also need to import the task class that we created last week. Combat O'Reilly, Diondre Task manager tasks. Now we hold a reference to a collection of all the tasks and that will get passed in at some point. And, um, the best place to pass that into the task list. Adaptor will be the constructor, so I'm gonna implement a constructor like income. Appear to source. Ah, generate constructors. Using fields is a There's a nice little eclipse. Um, timesaver. So then that will create a constructor using all the fields that we defined so far on the on the class and weaken, pick and choose those. But there is one of the thing that we need to pass up to the base adapter, and that is a context to deal with. So I'm also going to define excellent clothed this Well, here, let's look at this. Ah, because this will show us the signature up the chain. It looks like there isn't one, so we are going to need Okay, I actually misspoke. The base adaptor there's there's a different adapter that needs a context. But since this base adapter is really just a simple abstract class with not a lot of implementation, we have to deal with the context. Um, and, uh, we'll talk a little bit more about different types of adapters later on, but we're gonna need this Ah, context as well, because we need to construct views against the context. So I'm gonna add another field here that will allow us to generate our constructor that we want and a context Ah, Clips doesn't like me doing it that way. So I have to finish the statement first, and then it will complain. And then we can import Android that content on context again, The context is Aziz started hinted at in earlier classes a, uh well, for instance, and activity extends a context. Context is sort of Ah, the ah holder, a placeholder for a whole bunch of different things that need to happen with android Ah, activities and views and stuff like that. And ah, all the way up the chain and activity, um is a context and that's so we could pass it on activity here. And then we'll have a context against which we can create views and things like that, which you'll see in a little bit. So now we have these two fields that we need on the adapter. I'm gonna go back up to this source menu and say, generate constructors using fields, and I can say that the fields that I want to initialize this class with our two tasks in the context that tea fields that I just created here and I'm gonna say OK, and there now we have a constructor. Tasca said, after which will take an array list of tasks and a context and then set the fields. And now we can start to think about implementing some of these other methods. So since we have an array list of tasks, which is going to be the day that managed by this adapter, um, can anybody guess what we would want to return from this get count method the size of the list? The size is what I have here to what's good. There we go. That's exactly what we want to do. So the count of all the, uh oops I'm in, I'm in ruby mode. The count of the number of pieces of data that this adapter manages is just the size of the list. So exactly that's only one return. And similarly forget item. We want to return given a position. We want to return the actually to do it this way. Kind of a nice little guard. Uh, I don't even know if this is needed but kind of defensive programming. I'm going to say if the tasks or know that we'll return No, instead of blowing up otherwise, we'll get possession of ah, the tasks And then just for our purposes, Um, this is totally fine to doing an override. I'm gonna be more explicit about the type return from good item because we want to return a task. Not just any old objects of this task list Adapter is only set up to return or to deal with lists of tasks. A, uh and I'm a ma Jenness collection. I think that's called now, Um, for the I d. Like I said when I kind of went over this a little bit earlier. The I d really only needs to uniquely identify the items in the list. It doesn't need to be anything special, like a data base I d or something you may find in some external location. Um, so that's good enough for me because I know that tasks are not going to be duplicated throughout the list to just return the position here, and that's good enough to uniquely identify any item. Now, this last bit beget view. Um, we're gonna come back to that in just a second. But ah, that is where the adapter is responsible for displaying or for forgetting an instance of the view that the list view will will display for each individual item when it's asked for. So let's see, what's the next thing to do? I'm going to start wiring up what I think the view should look like, and then we'll come back to this, get view and ah, wire up the code that will make that actually get called. So, um well, first, let's look at what it should look like inside of the slides. So we want, um, as you can see here, there's only one item in the list that list item should look like, um, just sort of Ah ah. But a text over there on the left, left, aligned, um, which is the title of the task and then on, right. Aligned, um, up against that is the check box that will display, um to let the user know whether or not they've completed a certain task. So I'm going to first create that layout. Inside of resource is layout. I'm gonna create a task list item file. So I'm gonna go right Click that, say new file task list item. I'll call it that XML. Okay, Got some complaints there that aren't really important. But inside the XML, I wanted to look like this in the instant. In the interest of time, I'm going to copy and paste something. Actually, let me start out like this so I don't so I can talk about this in the right order. I'm gonna pace the insides of the file first. There. Here's what I want the insides of this file of this container to look like the first Ah, item. Um, I'm showing you here, um, another instance of this text, Um, this use of ah, sort of a magic I d or an idea that's been defined before by android. And this is kind of in a different way. It's defined against this android colon I d. Type of identify rare, which is slightly different than what we saw before. Basically, this component, this checked, checked text view component has already been defined and is used by the android system elsewhere in its applications. And this is what I've I've got out of ah, of, ah, different foreign posts and stuff. I'm not really sure where I found this first, but this is the easiest way to display exactly what we want to display here. A label in a check box. So instead of doing that, we could manage check box component, um, with some text and make sure that the two don't overlap and all that kind of stuff. But there is a component that sort of, um, already used that does that stuff for us. And it's called a check text view. And what's weird about it is that there's this check mark attribute on here. That all right, let me kind of dive into this explanation because this is weird, and I'm not really sure. Um, of a better way to explain this to you other than this is Ah, this this android attributes type of a notation with a question mark in front of it is Ah, it looks up dynamically. An attribute named list choice indicator multiple in a list of attributes that android keeps about, um, the system, um, up in the the innards of the sdk, I don't really know where to get a list of these attributes unless you check out the source code for the android sdk, which will be doing much later in our in our, um, classes. But basically, I found us online somewhere, I think, and then and spelunking around in the code of it, I was able to understand how this all worked out. But basically, what I'm doing here is just using this check text view component, which is exactly what we want, which has a label on the check box. And this is the way to get the check mark to display correctly, and we're gonna center it vertically, um, which will make it not set up against the top and the lead with this Phil parent and the layout height, there's rap content. So, um, hopefully that's not too weird for anybody. Doesn't weird anybody out too much. Um, but this is this is the way to get that component set up the way that we want. So now what we could do to make this. Ah, since this is really all we want on the screen, we could use something like a linear layout or something to contain this component inside of it. Um, but There's some other things that we need to do programmatically to this view. Like, for instance, later on when we store whether or not a particular task list item has already been completed or checked off, we need to change the checked, um, the checked, um ah, attribute of this check text viewed Teoh to make the check box Come on right away. So there's a few things we need to do programmatically and the way we're going to do that is instead of instead of defining something like a list, um, are like a linear layout or relatively out or whatever here, we're gonna define a custom layout and the way that we do That is just what you see here. I'm gonna surround the component that we have here, Um, from line 7 to 12. Now, with a custom XML tag which points at a custom view class that we're about to create and you can see that the way that you do that is by putting the fully qualified path name to the class that you want to use and that needs to be in it. That needs to be an instance of view, So we're gonna go create this in just a second. Um, and you can see that I have this orientation, um, attributes on here, so that should hint to you that what task list item? The Tesla's item needs to be an instance of you to be a something that can sit up here, um, as a tag. But since I have this orientation, that should hint to you that the instance of you that we're gonna uses a linear layout and then layout with the layout heights are Phil parent. So let's go and create that class. So inside of there. Well, so that's that. Maybe a little bit weird. Let's stop to see if any if there's any questions about what I'm doing so far and ah, I'll just tell you if there will be better answer by going further in the code or not. But is anybody interested in anything that they see here? Tony, I would be very interested if you could just get kind of give a quick summary of again. What is this view that you're building specifically? Is it for people to enter the tasks or is it for each task in the list? It is for each task in the list. So it's It's just this view to display a task. 11 individual task in the list of all those tasks. Okay. And so so Because we want to put text our own text. That's why we're having to create a custom view. Well, no, just to make it easier on us, because what we could do instead is created text view and a check box component. Um, I could have a text view here and then have, like, a check box component here. I think it's called checkbooks. Um, and then I would that I could manage the layout to make sure, like the text view when when the text of the task got long, it didn't overlap the check box or push it off to the side. Um, I could do some more work to do that. Um, and I think that could be accomplished by a more complex layout than just the linear layout. But what I found by looking at how android applications that use the list view with a check box in it, um, accomplished that you used this component, which is exactly what we want, So I just decided to use this one instead. And whenever you run into this problem in the future, you'll have the code. And then you'll know that you can use this just the same way that I haven't. And so you can sort of suspend disbelief about this crazy sort of syntax here online. 12. Why's the orientation? Horizontal Shouldn't be in portrait mode. Will know the choices here would be horizontal or vertical. But since there's only one component here, it really doesn't matter. I think that maybe sort of a legacy. So it's a vertical. Same. Same difference because there's really only one component in here. We don't need to worry about that layout. So make sense. Yep. Okay, cool. Exactly on the word you just highlighted there. Um, who's allowed to be used as XML tags there. What? What classes that that we invent in our project are allowed to be used as XML tags ones that extend views? Yes, because this is a layout file after all. So it's something that has to be laid out on the screen. So, um, it has to extend view. I have a couple of questions from the chat room here. Um and their most listened to be dealing with the adapter pattern. For instance, why did you create a, uh, another package for the adapter when there's already a task package? And also, when you're creating a list, are you going to have to extend the basic after for every list that you create? Um, well, seem to be good questions for the audience at large. Okay, um, there's there's been a couple different questions about why would create, um, new packages. I mean, it's just an organizational thing, but I just, um especially on a couple different products that have worked on that had a lot of different lists with a lot of different types of use for the items in the list. Um, we've needed to have a lot of adapters, So I put those out in separate packages so I can keep all these actors together, and I could know where to go to look for adapters. Um, it's not that big of a deal beyond just just ah, just sheer organization. Um, and then the other question was, Do we have to expend base adapter every time? No. In fact, the homework that I'm going to assign you will show you will point the way towards an easier way to do simple lists. The reason that I'm using base adapter, which is the the well here, let's read the documentation for it says it's a common based class of common implementation for an adapter that can be used when both list view by implementing the specialized list adapter, interface and spinner by implementing the specialized spinner adapter interface. So it's a very simple basic adapter that you have to do a lot of code yourself and the advantage for this glasses showing you what kind of code that the adapter generally has to do. It has to do this type of a thing. It's until talk about the account, um, get specific items, get unique identifiers and then, um, constructive you for for list item. So it shows you how an adapter works. And that's the advantage of of working with the basic after here, okay, and then I have a couple more quick questions. Somebody was confused about the orientation issue again. If you could maybe explain that a little bit. You're clearly a couple of people are confused. Actually, the other two questions are what exactly is schema as dot android dot com for its lush a PK slash rez slash android. And the third question is, How do you know which takes to use for each implementation? Um, so the orientation thing is, Ah, I don't Do I have a linear layout? I think I do in ad task six. Email. So this is the screen. This is the view for the screen where you add a task when your layout, um either lays out all of the child views the child components in a vertical or horizontal direction so I can choose whether it lays things out horizontally or vertically like in this case, when I go to add task, things were laid out from top to bottom, so that's vertical. If I wanted to get laid out left to right, then I would say horizontal. And what I've done here is go ahead and say that the orientation should be vertical because what is gonna happen is this task list item that we're about to create is gonna be a linear layout. And I guess the question that Beth had was why was it horizontal? And my answer was that it really didn't matter because we only have one child. And so there's nothing. It's not. It's just gonna take up the whole view. But we do have to define an orientation for the Litan for the linear layout. So I just picked vertical, and I think the horizontal was actually left around in the code because I was trying to do a text view next to a check a check box like left and right. I was having trouble initially setting up a text of you laid out to the left of ah check box, which was hard against the right of the view, with the text of you label overlapping the text you are getting cut off and not not ah, lip sizing the right way and stuff like that. And then I poked around a bit and found this instead. So that's probably why I still had that orientation horizontal still hanging around in there in the code. But really, it doesn't matter because there's only one shot component. So, for that answers the orientation question. Thea other question was, What is this? The name space, the XML name spaces, Air kind of Ah, you can look those up on Google. Andi, find out way more than you want to know. It's a way for you to have different. So except the name space that we have here is android. And basically this doesn't need to be anything special is just unique identifier that will that will distinguish it from other name spaces that we use an XML file. Um, and that allows us to do this android colon thing here for each one of those attributes, which is a bit verbose, but it allows the, uh the whatever code is is ah interpreting this XML to pick out specific attributes and point them, um, and check against, like, somebody asked earlier, If we're going to writing a d t. D for this, it allows to make sure that these attributes are legal And if they they match against what Andrey is expecting for certain attributes to be there. We need that because we're implementing accustomed custom view. Essentially, no. If you look at add tasks dot xml, here's the name space that appear. You need that because this android colon thing is special. I see. I hadn't I hadn't noticed it in the linear layout before. So now that makes? Yeah, probably. I just had it sitting up here at the top because, I mean, it's just line noise and for me, you know, because I don't really care about that. I care about getting the view set up, so I kind of get it out of the way. But, yeah, it popped down here to the bottom because this line got so long. Yeah, it needs to be in all of those xml files. Well, looking through the documents documentation on the android site, we'll let you know which ones are possible. Um, so I was trying to you like, for instance, text view. We've already gone through, um is a pretty common one. Um, and then check boxes. Another common one. I found this one by looking through android source code, the source code for the apse That that get ship with the android? Um, platform. Um, so but but it's in the documentation to you could go look up the and the Google documentation right now for check decks for you. And look at the That s so basically, the answer is by looking at, I think the best answer for me, It's It's not for everybody, but I think that the best answer is to look at source code as much as you can. But but all these air upon the android docks so you can kind of poke around there and figure out um, maybe find some new ones over, see what's there. Or you could listen to screen casts like this and have somebody else tell you, Ah, which which tags that you use for creating different types of applications. Thanks a lot. I don't know if it was that the too much of a trade answer. No. Uh, no, I'm just asking. Posing. These questions are coming through the chat room for multiple users, and I want to make sure that they're addressed not just in chap, but also through the course. Cool. Yeah. So, yeah, this is this is what android developments all about. It's figuring out which of these different options that you have, All right. But then you also have the option of creating custom tags and pointing them at classes, and that's exactly what we're gonna do next. So we need to create a task a list item that sits in this package structure. So I'm going to you create yet another package called views and put a class in it. So I'll do that through this new class wizard here, um, package of to say is views. And we would do this just because I want to keep all the views in one place. Oh, are having another separate hierarchy there To keep Teoh. Keep it clear where views are stored. The name needs to be task list item. That's the That's correct, right? Task list item and the super class is linear layout. And if I hit control space here that will get completed to the class that Eclipse knows about which is android widget deadly nearly out, Which is exactly what we want. Finish. And now we'll have this new class, which will be the implementation of that view. So let me look at what we've got inside of here. So I'm going to treat this linear layout just as I would, um, inside of a ah sort of think of this is, um, like in a model view controller type of a situation. We're kind of breaking down a little bit. Were the view logic is because there's a lot of you logic that's taken care of by Android. Just by virtue of being a linear layout and having that XML file, define what components we have on the view. So think of this class more like a controller, I think. And and it should have the responsibilities of the controller. Um, and the response responsibilities that the controller would have in this situation are, I think, holding onto the components that you need to interact with and one of those components is a, ah checks text view. So we'll have a class for that or a field for that checked text. Do you? We'll just call it check box because this will work more like an HD mill check box where you can have a label as part of the component. Somebody implement that guy, check text to you, Andrea dot widget. And I think the controller should also, um, know about the data that it needs to display. So we're gonna have a task called Task and we need to import that guy and I'm gonna collapse these imports, save us some space, and, um, now ah, Andrew are eclipses complaining that we need a constructor because there is a super constructor up the chain that needs some more information. So I'm gonna implement I'm gonna hover over this task list item class that has the problem and say, at a constructor that takes a context in attributes set. So this is sort of a view into the innards of Ah, what? Ah, what those what Those things that we define in XML need they're actually end up doing Java code. You get this context because the view is going to be, um, as you will see in just a little bit inflated, which means red from XML and turned into a specific job a class defined by the tag at the top of the view. And then, um so that context is important to till till the view some information about the situation that it's being inflated into. And then there is this attributes said, um, which corresponds to the set of attributes that we've defined in the XML. If I go back to the XML file, those things air passed in that way, then the inflator deals assault with all those things, and we'll be looking at that in just a second. Um and then we need to send those variables up to the super class when your layout and then that's pretty much all we need for the constructor. Now, um, what I also want to do here is allow someone to set a task on this on this list item and when a task it's set will perform a couple different specific functions. So I'm gonna hover over here and show you nice little, um ah. Helper function and eclipse that will allow you to, uh, generate getters and centers for a field. And you can see that there's, ah, yellow underline underneath task right now which says that nobody uses this. Um, are you sure you want to define it? And one of the possible ways to fix that is by creating a getter and setter for task. And then we will allow, um, someone to set a task on this class, um, through set task, and then we'll just have get to ask for, you know what the heck? Click OK, now you can set a task and get a task and task is no longer warned are not knowing as a warning associated with it. So what we want to do when we set a task is not only keep a reference to that task, but we also want to manipulate the check box that we have a reference to based on the data that's inside of that task. And if I paste in some code here inside set task, you'll see that it all kind of makes sense. What we want to do is set the text of the task. Teoh are the sent the text of the check box to be the name of the task, and we also want to set the Czech state of the check box Teoh whether or not the task is complete. And that is a new bit of implementation for the task because the the task that we have right now doesn't have a notion of whether or not it's complete or not. So what I'm going to do is this is again a. Some people have commented in the forums I like to write code before it's actually defined and sort of work work through that way, especially in a screen cast situation. So I'm expecting there to be, um, a method called is complete on task, but there isn't one yet, so I can hover over this and say Create method is complete in type task and it will do a little bit of work and then pop up the task definition. Here and now we have a new method. Cold is complete, which returns a Boolean and tells whether the task is complete. So right now it's just using this default implementation. But now we wanna have a notion of being complete. And we'll do that by defining a private variable here, Boolean complete. And now you can set where you can see you can sort of query the task to see whether it's complete what you can't say, whether it's complete yet. So we have part of the job of being type of pattern. Set up is complete use is instead of get for for instances of boolean variables as part of the job of being convention, which you can read about elsewhere. But we also need a center for that. So I'm gonna come here and say, getter and setter for complete Um, you could see the get her name is is complete, and that's already, um, generated by that previous step that we did. But the center name we want to be set complete, and I will click. OK, and now we'll have set complete function for us so that we can say whether or not a task is complete. And now we have an idea of completeness on task on, and we just need to return it here. So return complete now a task knows whether or not it's complete. Now back in tisk task list, Adam World set task is set up. That's what we want to dio. Um, whenever we set a task is get the name, display it get whether it's complete and check the check blocks accordingly. Now there's one other thing that we need to do. You may have noticed that we don't have a reference. We don't ever get the reference, um, set up. We don't never link this this check box variable up to the actual text check box component, checked text or whatever it's called. So where you want to do that inside of ah, linear layout? Is theirs sort of a life cycle to when a view is inflated? Um, and ready for for ah, all of its components are in place, so it may be it may seem readily apparent to that You to you that you'd want to do this inside the constructor. I would want to say something like check box is fine view by I D In fact, here. I didn't just copy in the code that we want, but ah will explain to you why you don't want to do it in the in the constructor in just a second here. So here's the code that we want. I want to go get a reference to this check text view that we have out on the screen with the fine view by a. D. The same way that we do it inside of activity. And it will use this android that are instead of the are that we have. Because once again, we defined the idea of that using the text one identify air that is used elsewhere in other android applications, and that looks like this inside the code. Um, that's a text one I d. Against the android are, And in any case, this this code will get us a reference of the check box reference to the check box. But the problem is that during the constructor, none of the child attributes are ready for reference yet, So you would actually get an error here and if we had more time and I can actually show you getting that error, but I don't want Oh, just take it as as, ah fact that you would get an error if you if you tried to reference any of its child views inside of the constructor. So what you want instead is to use a method that is called when the and when the view is finished inflating, and that is called appropriately on finish inflate. I just pasted in code here from 19 to 23. Make sure you call super always when you're overriding things, unless you know you shouldn't. And then we'll have that same code that gets us the reference. Now we have a reference to the check box, and a task list item can can, um, correctly set the text and set the Czech state for the check box. Um, when a task is set on it. So now that we've done that, let's go back to the task list adapter because its responsibility after all of this other responsibilities taken care of, is to get the view set up for a, uh, a particular list item. So there's a lot of code that is gonna need some explaining here. I'm just gonna paste it in and then talk it through line by line instead of doing a lot of typing. So inside of give you I'm pasting this code is completed code. Um, that does some interesting things. So ah, the arguments to get view our position because the list view is gonna want to get a view for an item at a certain position. Convert view is an interesting thing that we'll talk about in just a second. And the view group ah called parent is this is basically a view that it's gonna be rendered into, I think, in some situations. But in our case, we're not going to need to worry about it because, um, just in in this case, you don't need to worry about this. This last item here, I don't really have more interesting information to give you about that because I always use this. This sort of set up in the situation where you don't care about this last argument. So there's nothing to talk about their really in our situation. Um, but what we want to do is create a task list item view. Uh, first, we need to import that guy. So I'm gonna over over there and import it. And now let's talk about convert view, because this is an important task that a list for you needs to do. And that is, um so say we had a list with, like, 1000 items in it. Ah, if a list of you created a list item for all of the views that would need to ever be displayed for that list, that would be a huge performance. It would probably crash the application because that would just be too much memory. Two in Stan. She ate all those views. So instead, what it does is as you scroll through the list and list items, um, sort of scroll off the end of the list and are no longer visible there. Actually, um, they're actually recycled in sort of, ah, situation where anyone's, there's there's a pool of of a fume or than needs to be displayed at any one time. And then as new ones are recycled or for ah, list of you sort of go off the end of either side of the list of the visible part of the list of the bottom of the list of the top of the list. They're recycled to put back in that pool and then called back in when there needed to be displayed again later. Um, so that way that the list of you can make sure that it only in Stan Shih eights, um, like a small enough views to be seen for each list item. And the way that this situation works is that a, ah, a recycled view for one of those list items is passed back into this. Get view a method here. And if the argument is no, that means that there was not one that the list of you wanted us to reuse inside of this adapter. So we will create a new one, and that's what we do here, which we'll talk about in just a second. But in the situation that a of you is available for recycling, we're just going to get a reference that view instead of in Stan Sheeting, a new one and will honor the the size of the pool of those list view items that the list view wants to create. Um, So what I'm doing here is just figuring out which one I'm gonna reference, um, either a new one or the one that thea the list view is passing to for us to recycle. And then after that, I get a reference to the correct one that I will call set task, which we've already seen inside task list Item will set the label and the the checked a state of that check box. Um, based on the data inside the list at a certain position, Um, that is also passed him to get for you. So then I will just get that data onto the task list. So hopefully that all makes sense. There's only one more thing to talk about here, and that is how to instance she ate a view from XML, and that is done by using the in flight method of the view class as a nice helper method. And this is where we get that inflator guy. The inflator again is responsible for taking the XML for review. Um, parsing it and then turning it into the correct Java class and returning an instance of you so view dot Inflate given a context and, ah, the task list item, Identify our on our class, which we also need to important here. Make sure import the right one commenter Riley Android task manager. Um, now it has an I d. Of a view that will it will inflate from XML and turn into whatever class, um, that is defined inside the XML, which again is a combat O'Reilly. That android task manager views task list item. And that is how we get the view for a particular item in the list. Um, that was that was a big chunk of stuff to talk about. Two. Has anybody have any questions about that? Before I go on? What's a big chunk? Somebody asked about the task being added to the task list item view and is now the view hardwired into the model. Um, uh, yeah. So the view shouldn't know about its model generally. Sure. This is, um I should get a to get information set on the controller. Um I mean, the view should be, you know, manipulated by the controller instead of know about its model. That's why I say that in this situation, I think it's better to act as if this class is sort of the controller for the view, which is sort of living above it as a linear layout. It just happens to be that this there, really? I would say that I mean, in this kind of situation, the controller is more tied to the view than the model is because I'm thinking of this task list item as the controller, and it's the one that contains the the reference to the model that the view needs to know about it. But there's not really a better way to do this. Where else am I gonna have a controller in the application? That is good at, um uh, setting the information about this particular list item. Um, that would be much tougher to do anywhere else. So I mean, this is this is ah, this isn't breaking too many rules in any way that anybody should care about. When it comes to, like good, good separation in your model view controller type of a pattern, Hopefully, that's a good enough answer and weaken. I mean, that's that's something that if we're interested in debating, we can take off line into the forums. Yep. Okay. Um and then a number of people have asked for a quick overview explanation of inflate again and exactly why you're doing what you're doing and where. Okay, So given that the task list adapter is responsible for a couple different things, first of all, listing the are having a reference that a list of all the things that needs to give to the to the list view on demand. And it's responsible for getting the view to the list view on demand. We need to. In some cases, in Stan, she ate the view. And because no one else there's nowhere else to say What list list item that we want to use. Um, but rather than this adapter, because its responsibility is getting the view to the list view. Um, there are easier ways of doing the building adaptors, which I'll show you sort of point you to during the homework. But, um, in this case, since we're responsible for getting the view, if there's no view to be recycled, we need to We need to understand she ate the view, and the way that happens is by saying that we want to use a view that is defined the or the layout of which is defined in this task list item guy, and we use a. But since we need to return an instance of you, we need to use the inflator to turn that XML into a class basically or an instance of class view. And that's what inflator does. It reads in XML figures out what class that is defined at the top of it and in stand states that type of class and then sets up all the attributes the way it needs to be set up. And then at the end, you've got a class of some type which extends view. And in our case, it's a task list item which extends linear layout, as you can see here. And that is why we used the inflation, um, metaphor idiom, I don't know. Convention, uh, code right here. Does that make sense to everybody? Makes sense to me. The next thing that I need to do before we can start start seeing some action here is to set up and wire up the adapter. So let's go back to view tasks activity. Um, and as it happens, my pointers exactly where we need to be. Um, after we set the content view two main, um, and we've set up all the views in the APP. Now, there is, um, a couple things that we need to do. So let's think about this. What we need to do is give the adapt we need to construct an adapter, tell the list for you to use that adapter, and then we need to at some point in their also tell the adapter where to get its data from. And, um, where to get its data is from the task manager activity because it holds that list of the tasks that we keep, um, up there on the application. Um, and we do that by using this. Get task manager application, get current tasks. Except the problem is that we, um also need to make another step here, which is to change what this activity extends. So we went through that whole that whole process of re factoring this tax task manager activity guy out last time so that we can call get task manager application, But now, because of the restrictions of single inheritance, um, in Java, we have to get rid of that test manager activity, inheritance and and instead, inherit list activity back to the T AC to the T. Did I do better that time? I think so. Okay, so now I'm gonna import that. Now, the payoff, the benefit that we get from doing that is that list activity is going to manage that list for us and make it very simple so that we don't have to worry about a lot of stuff like setting it up and, ah, finding it in the view. It's gonna use that that that magical view I d, uh, android list. Um, so it's going to do a lot of work for us, So it's worth it for us to do that. Except that now we can no longer call get task manager application, which is a small payoff, I think are a small, small trade off to make. So instead, we still need to do the same thing. We need to get a list, a list of all the tasks. Um, I'm gonna move this uptick into the controller and actually get rid of this method for right now because things are gonna be a little more slick. Um and then on resume, we're no longer going to call show tasks and then inside of the country. So I said controller, I meant on create. Um, here's the dance that we're gonna go through first, we need to get a reference to the tasks, but we can no longer do it this way. Um, we're going to. Instead, I'm going to get a reference to the application. I'll show you Sometimes this is a pattern that used I'm gonna have ah, variable called app. So we have a reference the application, which is a of type task manager application, and we're going to task that out of get application that comes with the activity. And now this variables not created yet. So I'm gonna create a field called app Minutes of type task manager application. So now I have a reference to that guy and offer that I can get, um, the current tasks, but I don't really need and in fact, it's a whole. It's a whole better way of doing things to not even have a reference to that list of tasks inside this view task activity, because I don't really need it. All I need to do is give it to the adapter. So instead, I'm gonna store a reference here to the adapter inside of an adapter of field which will construct right here task list adapter. And that takes arguments of, ah, context, which again every activity is a context. I can say this And, um, I need to pass up the list of tasks to manage, and that is found on app dot Get to current tasks. And now I need to import this guy over import. I need to make a field hover great field and now see, what's the last piece of problem here? You tasks activity. Not sure why it's complaining there and save. Oh, let me look One quick glance here. What ordered? I didn't find these and I have this different from my sample code. So I have the arguments reversed here. So I would just take this and move it to the end groups. This Okay, now things should be kosher. And now we have a reference to the adapter. And now the next thing we need to do is to tell the list view which adapter to get its data from, and that is done by simply calling set list adapter, which is a method on list activity. And we could set that to the adapter that we just constructed. And now our list knows where to get its data. And I think that we are ready to go on and look at some code actually running. I'm gonna clean up some of these unused imports here. So So look here. I'm gonna open this up in the emulator. And we already had two tasks. Defines Hopefully, those guys will get populated into our list right away. And no, they don't. So why is that? So we're not saving our tasks yet, right, Tony? So are they being so You're right? Yes. Sorry. They left memory so we don't have any. Okay, that's right. Good. That scared me for a while. I was thinking ahead to next week when we're actually gonna persist these so that they will stick around. But in this case, they don't. So if I come here and click, add task, get milk, are standard default. Hello. World task. That still doesn't show up. Okay, so I'm a little worried about that. Well, OK, so let's let's just take a step back and debug it and then figure out what's going on. So I'm going to put a debug break point right here and debug the app. Okay? And now it's asking to switch in the debug perspective. I'll say yes. And once eclipse wakes up a little bit from whatever it's doing, we should be debugging. I think I have maximized last time eclipses chugging along here. Okay, inside the bug. Well, this is unviable part of programming. Yeah, debugging is a good thing, Teoh to go through once in a while anyway, then when you've written so much code, this is really running slow. Yeah, especially when we've gone through a lot of code. So now I should be in the on create, and I'm getting a reference to the application. So what I want to do is step over that. So I have a reference to the application which should be on this, and here we go application inside app. We have current tasks, which is empty. Okay, good. That's what we expect after restarting the application. Ah, uh, I think I know what it is. So here's the problem. Um, what's happening is okay. Obviously it didn't occur to me first time. But once I restarted application, we have no tasks. So I can click resume from here. And we've got no no issues with why we don't have any tasks in the emulator just yet. But once I add a task Ah, and here's the line in my sample code that I didn't put in. You write it is the on resume. Somebody looked ahead. The guy who talked about theon resume looked ahead in May. Quite possibly. Yes, because I see now going into the Booker the kind I've gotta stop. Misty bugger. The comment is the adapter as to be triggered in the on resume? Yeah. Yeah, so I have that for I thought I didn't need to do that yet. And here I had a had a break point from last week, which showed when we had text being input. But I'm gonna get out of that, um, deal. Thanks, Neil. Okay, now, when I add a task and I go back here, I have to sort of, um, tickle the the adapter and make it say that we have a new data in it because there there are. There are more complicated ways to do that, but the way I'm going to do that here, which I didn't think I needed to do for this particular step. But I obviously dio is I'm gonna build in this little in on resume. I always want to get the updated set of tasks. So I'm gonna build in this little sort of paki way of doing this adapter that force reload, which is just a method that I'm gonna make. It's not any sort of system method. So now I'm gonna force the adapter to reload itself whenever and the on resume is called. So I'm gonna cover over here, force reload, create that method since it's not there, um, what we want to happen? Um, inside that adapter is we want to call this method called notify Data set changed. And that tells any any components that are using this adapter to a data provider that ah, the date of set has changed in that it needs to show its new data. So now, since that's being called on resume, I'm gonna fire this up again, and hopefully things will work correctly. Live coding, even when copying and pasting. Still a lot of danger to life coating. But that's all right, cause we're living on the edge here. This excitement. It's extreme programming. It's free. It's extreme programming. It's extreme programming in a real sense, too, because I'm getting so much help from the audience. My pair programmers. Okay, so now we have task displaying. Um, great. Um, we're gonna have more tasks, like, um, there is a task that I forgot. So now we have more multiple tasks splitting inside this list. Okay, That is the steps for this for ah, for completing this part of the process. And we have, like, three more to go, but I think they're a little bit shorter.

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