Skip to main content

Loading the Twitter Home Timeline

Lesson 9 from: Building a Twitter App

Tony Hillerson

Loading the Twitter Home Timeline

Lesson 9 from: Building a Twitter App

Tony Hillerson

buy this class

$00

$00
Sale Ends Soon!

starting under

$13/month*

Unlock this classplus 2200+ more >

Lesson Info

9. Loading the Twitter Home Timeline

Lesson Info

Loading the Twitter Home Timeline

So first up, loading the home timeline, what we're gonna do just quickly and this will be kind of a good refresher or or Well, in any case, will be going over a little bit of some stuff that we went through last class, lest the last time around where we built the task manager application when we went through a lot of list based stuff and creating list, um, list items for the list. And we'll do that for this home timeline list. And we'll show, um, well, format each individual tweet to show the the user name of the person who created the tweet and then the the tweet itself the contact, the content of the tweet and will separate them out a little bit. So it'll be a little bit easier to read, and we'll keep on working on that view over the next few classes as well. So to do those things first of all, we need to load the tweets, and that will be in a method recall. Get Home Timeline and Teoh manage all of that data. We're gonna create a list adapter, and then we're gonna create a custom lis...

t item and for those of you who attended the last class thes will, this will be a good refresher. So I'm gonna have over to the code and everybody should see clips. And you could see that there are five steps today, so I'm gonna go through the code again one step at a time. Ah, a new project for each step. So first thing we need to do in our list is to load the home timeline. And we already started to do that a little bit last week already. We already at least loaded the home timeline. We didn't really show it very nicely. So let's step back into the code there inside the status list activity, which is again that first ah, view that will we be opened up when the application starts? Someone opened up that. So if you remember last week, we loaded the tweets. Actually, what we did was this would decide if we were authorized or not. If the if the user was signed in, based on whether or not we had the token for that user. And if we do, then we're going to instead of going to the authorization flow like we went through last week. We're going to online 37. Loan that load The timeline if it's not already loaded on line through 45 is where we decide. Um, if we want to load the home timeline or not, and so we don't want to load that every time that we come back to the status list activity, because that would be kind of distracting. So if we already the way we're gonna make the choices. If we've already got a list adapter set up, we're not gonna reload. Because then we can allow the user to load new tweets either before or after what they're seeing with those headers. So we're only gonna make one load. Um um, but depending on if we're if we have this get list if we have the list adapter already set up, which will show you in a second, So if the listed after is no, we haven't set the list of after. We shouldn't have any data in the lists. We're gonna call this load home timeline method, and that is down on line chicken and status list activity. So what we're gonna do is call out to Twitter which remember, is the Twitter for Jay Twitter class, which is basically our A p I access class Get home timeline and that will get the home timeline, which is what you would see on twitter dot com if you were signed in and it gets it for the currently authenticated user, and, um then we're going to create a status list adapter and we'll set the list adapter to that adapter, and then the list of you should then populate. And from then on, we will, uh, will no longer load the lone time will no longer load the home timeline when we come back and resumed that activity. So last class, everyone should remember dealing with the status with a list adapter list. Adapter is again sort of a data provider for an android list, and we're gonna make a custom one, and that knows how to to ah, display tweets. So I'm gonna cover over this online 55 hit F three, and that will bring us to the class to the classes constructor. So what we're gonna make is a very simple adapter that extends an array adaptor and an array adaptor already knows how to to, um, display a job that you till Not a realist. And, um, that's nice for us. We don't really have to do a lot of overriding if you remember. Last class, we dealt with all of the with implementing all the methods that are defined by a basic adapter, I believe, which is up the inheritance hierarchy. But this adapter knows how to deal with a raid up or with a ray lists, which, which makes it easy. We don't have to implement a lot of things. Um, and then we can pass in a generic. We want to want to deal with a raise that are full of statuses, which are again individual tweets. So Ah, this constructors pretty standard. Um, for an array lists or for an array adaptor, it wants to have a particular view that it will show for each individual item, and then it wants to have full collection. So let's go look at this view really quickly in the project. Under rez for resource is layout status list item. Oh, wait, no, we're moving to that yet. Sorry. Look, a simple list. Item one. Oh, I see what I did here. Ok, sorry about that. That's were passing this just kind of as a I got a placeholder. I forgot that. Um, we're gonna be customizing the view completely with get view right here from line 23. Deadline 33. So forget this. This is just too I think I copied it over from the code where I was just showing the the two string output of each each status. So give you this is what the method that's called when the array adaptor has to show a new instance or a new item out of the array and, ah, it will pass in the position of the item that it wants to show. So this is an And in the words were returned a view from there. So this is the This is the code that actually says, What've you get shown for each individual item of the list? All right, So, um, let me go over this again for anybody who wasn't in the last class or for kind of as a refresher. There's if you have a whole bunch of data for a list in any sort of any sort of list, you wanna, um, there's there's sort of similar code or or similar conventions to deal with this on the IPhone and in other types of list of you type things. Um, if you have a whole bunch of data, but you can only see a few on the screen at any one time, you don't want to make a whole bunch of you don't want to make a view to show each individual item in that list. Um, because those those views are just gonna be, um, taking up memory but not showing anything on the screen. So what they what a good strategy is just kind of recycled his views and only show, um only construct as many views as can be seen or maybe one or two more. So that way, each time the user scrolls the list, the list component handles saying, OK, one of those views just moved off the end of the list in one direction. So let's recycle it, moving around to the direction that the user is coming. So what happens in Android give you will be called by the array adaptor when it needs Teoh get a view to display particular item at a particular position in the collection of data that it's showing. And what will happen is if it has any of those views ready to recycle. It will pass that in in this this ah argument here. Convert view. And then there's this view group parent thing at the end that we're not gonna actually make use of in this class. But the question we have to answer is whether or not to convert view is knowing if the convert view is no, that means there's not a A list item that's gonna get recycled. We need to create a new one. So we're going to inflate that out of the you speak calling view that inflate that will find a layout, an XML layout and then turn that into a class. So now is the time when I go look at that status list activity. Our status list Item inside rez for resource layout status list item dot xml. That's the layout for each individual view. Um, you can see that it is a custom Lis are custom view, and we we tell Android it's accustomed view by pointing to a particular, fully qualified class name of a class that extends some subclass of you, So we'll look at that in just a second. But on the on each item, we're gonna have an image of you to show that avatar of the user that created the tweet. We're actually going to load the avatar this class, but it's at least gonna be there for a placeholder. And then next to that will show the the users user name. You could see that its layout laid out to right of the image of you, which is an idea of user avatar. So let's it's appear. And then the text view to show the tweet will sit underneath. So laid out below here online 24 the status user named text and then also laid out to right of online 25 years. Easier Avatar. So that will be the layout for each individual tweet. And you could You saw that it extended a view class. So let's go look at that class, and that is in ah, calm down. O Reilly, Diandra Dido tweet dot layouts status list item And that class you can see extends relative layout, which has allowed us to do the things like the layout next to or layout layout to leftover layout to right of way out below, um, laying things out on the screen relative to other items on the screen. But this view is pretty simple. We just give away to set set a new status on the view, and leading a status is a tweet tweet, sort of. The informal name status is the formal name, but when we pass it a new status to this view, Um, this is when we set all the the components data. So the screen name the user's name will set whenever the status is set. By getting the user off of that status and then putting that text into that component and then the status text will, we'll do the same thing there. And then in between there, we call fine views where we do the this standard find view by I d Dance, where we load the components that we want toe manipulate on the screen. So that's a status list item. Just as a few pieces of text that we show and a way to set a new status and set status will get called down here back on the status list adapter inside the get few method. Um, once we've decided online 26 3 Line 30 Whether or not waiting to inflate a new view or just simply use the view that's being passed into us, that's getting recycled by the list. Then online 31 we set this status to the item that we're getting out of the adapter at the position that were configuring interview for. So this is all kind of standard stuff. Um ah, if we went through it a little fast, you could ask some more questions on the Forum. But this is what we spent a good bit of one of the classes last time around on, so it should be pretty familiar. When we load this, we're going to get a list full of tweets for the current signed and user, and then each item will show up. It looks like I'm already running, so it's not gonna actually load anything new. So here we go. This each individual item has a the tweeters, um, these air name and then the content of the tweet and I can scroll down to the end of the list and its recycling and calling get view each time. One of those views, moves off screen and then get down to the end of the list. And there it ISS, That's then. Okay, so I think we've successfully done everything that we want to do for this. Step around. Let's make sure, yes, we've created a listed after the adapter is the data provider for the list, and we created a custom list item and then loaded it in that get view, a method of the list adapter. And we have successfully conformed to the to the right strategy that Android uses to reuse the list items in the list so that we don't have a whole bunch of memory taken up with useless views. And in summary, Um, what we did to kick off this whole thing is that Twitter for J will load the authenticated users timeline with load home timeline. So that's what we did to populate the date of the list that we see so far. So again, that's pretty standard. Um, sometimes review stuff, um, dealing with android lists. Um, but are there any questions about what we've gone through so far? I have one question. I think it was from somebody who missed the first part of the course they were asking about array adaptor. And if that is specific to Android or if it's more General Java Nope, that is a That's an android dot widget daughter Ray adapter and hearing me right, click this and say, uh, show type hierarchy so everybody could see that you zoom in here. Um, well, Hannah Museum over here, you can see our status list adapter extends array. Adaptor, which extends base adapter and based adapter, is what we, um, implemented is an abstract class that you need Teoh implement If you want to take care of a lot of, ah lot of the things that have to do with an adapter again in a data provider for a list, that's what we did last time. So we had to implement a lot of these methods. But in ah, an array adaptor knows how to deal with a ray list. So it's, um, that's better at dealing with those kinds of things. And yep, to answer the question, that's ah, standard android component. All right, um, some he's asking if we should know out of you to free that view when it has scrolled off the screen. Ah, Well, this is just our reference to the view. Um, I think changing the reference is enough for And this is this an android or this is is this an IPhone developer? Because I've started to think about this stuff a lot. Maura's. I've gotten an android or IPhone development where you have to deal a lot more with hands on memory management, I think changing the changing, the reference of the V variable Well, that's That's a local variable. So that goes away now. We don't need to really deal with anything like that in any way. The list is already dealing with that pool of views that it's gonna recycle from, So it'll have references, everything that it wants to keep. Okay, and then the rescue garbage collected. All right, we have a question. Shouldn't we call find views in the constructor of status list item. Let me open that up. Um, yeah, that's a good point. I don't know what happened there. That's probably a lot better so that it's not calling it every time fund by find view by I d. I don't know how expensive that is, but yeah, that's a good point. Good cash Thank you. That was Michael Robert asking that we have somebody in the chat room who noted that moving find views, crashes the app. So maybe that wasn't such a good idea, huh? Yeah, it's be good to test. Um, why did I do that? You might have been expecting that something was there. And it's not in the constructor users. Oh, you know what? So the right place to do it is not in the constructor. And I think we might have done this last time. It's all it's coming back to me, but it's hazy. I think we want to do this in a I'm gonna go to override or implement methods. Okay, sorry. Let me start from scratch. Um, I'm hitting command option on the Mac. I think it is Holt, um, shift, perhaps, on windows. Either that, or you can go to source and say, override or implement methods. And then here all the methods that are available, um, to Thea to the type hierarchy that we're that we're implementing here. And I think what we want maybe under view, um, doing this off the top of my head on finish, inflate. I think that's what we want. So I think you want to implement on finish inflate, which is a callback method that is called during the life cycle of a view which will be, um, during the point after the views inflated. So back here in the status list adapter, remember, we're inflating a view, which means turning an XML, um, XML layout into an actual usable Java class. And this will be called after that inflation is done. And at that point, all of the all of you should be available. So I think why it was crashing in the constructors because we tried to do a fine view by D. But there's is probably doing a no pointer exception or something like that, I don't know, but those views aren't available to be found, so we need to call, find views after unfinished inflate, um, to ever tested that last thing. You want to try this? Or maybe I can now that we're we're going off on a sideways path. But that's fine. That's what the value of these classes are can respond to these kind of things. Okay, switch over and no crash. So I think that's the right thing to do? Yes, I do it using that last time, the on finishing fleet. So now it gets called not only at the right time, but only wants purview. So that's much better all around. So that's what the coach should be. And I can I can check in in a a new version of this code to Does everybody understand why? That's why that's probably a good idea. Then we're not calling it every time you sit the status yet because you just need to do it once. Okay, Makes sense. Um, Simon asked, Is there a case when you would use a list a doctor instead of an array? A doctor? In this particular case, I guess. I don't know. Let me let me look up what it listed after, uh, we use a listed after last time for something, you know, not to think about it. We may have in that extends base adapter. So listed after I think is the is the one that you want extend not a base adapter. If you're going to be doing all that, all that work yourself. So I think that's what we might have done last time. Now that you mention it. Um, in this case, I don't think that we needed to have What was the question? Whether we should extend. We use it for this class for this, For this particular case, I think, Yeah, I think they were curious if you could use a listed after instead of an array adaptor in this case. Yeah, um, I have to refresh my memory on what exactly was stepped adapter did. But I think you're right about that. Might have been what we actually extended last time. In this case, I don't know. Less code is better as far as I'm concerned. So this is exactly what we should do for this case. But, um, it might be better than extending base adapter if you have some special needs. Okay. And then last from me. Um, somebody wanted just a quick recap on the I DS. Do they need to be unique across all the layouts or just within a single layout? Um, So what they're asking is ah, status list. I'm gonna open up status list item. XML layout. So what they're asking about is does this does this particular I d. I need to be unique across actually let me just start from the beginning in case anybody missed the first class. So what this is saying here is we've got a component. An image view class. Um, and then it's getting laid out on the screen and to reference that in the Java code, we need an I d to grab ahold of. So this android I d, um, property here Her attribute of the XML, um, registers and an I d with the with that view that you can grab hold of through a special class here in the list item. You see that? It leaves us a reference on this on this class called our that we can use to grab the status text or the status user named texts which are set up down here. And then the syntax goes like this you say, at plus I d when you want to create a new one and that will auto auto increment the number or till until android to come up with a new unique identifier. So, um, the answer is this doesn't have to be unique across the project, but it does have to be unique with any any view hierarchy. That's loaded by an activity at any one time. Right? So that's all the questions I have. Sarah and Ruth, do you have any before we move on? Yes. I have a couple of questions. This this roof here. Um, so somebody was mentioning that in the status list item lay out there was a second plus online 24 the I. D. Is defined earlier. Is this a bug? Uh, Line 24. Plus status. User name, text. Oh, interesting. Yeah, that that's That's a book. I'm not sure how bad of a bug, though. I think we'll This this proves the This proves what I said earlier that you can have that you can redefine. I ds there. You can have multiple I d. S, um, in the same project, cause it's not era ring out, but it does. It kind of sounds weird when I say that you can't have more than one or you have to have only one purview loaded by on activity. So how much? I'm not sure why that's not blowing up. In fact, I'm not even sure why it's laying it out correctly like that. But anyway, it should look something like this. Um, I'll have to look into that. And we can talk about that on the forms if you're interested. Um, but apparently you can do that. Okay, um, did catch good catch hunting. Um, and then somebody came in and said, I'm confused on view dot Inflate and it being cast a status list item. What those few dot inflate dio have you done? Inflate takes an X remotely out like this. Like, for instance, status list item maximo and turns it into a Java class by parsing XML. Well, it doesn't actually parse XML because, um, there's a part. There's a There's a step in the pre compilation phase where this has turned into something in binary that's XML has turned into something binary. But it's not, um, a Java class yet, so that needs to be turned into a view, which is a class that the Java class that android can actually work with, Um and basically it just reaches what the definition of this thing is and then turns it into whatever class it's extending. And the way you to say which classics it's extending is help here on line two in the root XML. Whatever whatever class you have up there is going to be the the type of the class that the view extends and gets turned into when it's inflated. Is that all? Making sense? So I'm all set here. Thanks. All right. Yeah. I think that's making sense. Tony. Thank you.

RELATED ARTICLES

RELATED ARTICLES