 Hey everyone, so this is the last video for working with the firestore last time We got our data out and we were working on displaying it in a list And we do have that going on here, but our list isn't very pretty and maybe we want to customize it a little bit In the recycler view you did this with card views in this we define we do it a slightly different way Okay, so the reason that we're kind of stuck with this ugliness here is When we tell our list view how to display we say here's your adapter And I want this adapter to use this simple list item layout and predictably the simple list item layout is pretty pretty simple So if we want to make this more complex what we have to do is define our own Array adapter and replace it in here So that's let's do that. It's actually not too bad to do What I'm going to do is I'm going to go ahead and make my new class Inside my activity, so I'm going to create an inner class You can absolutely 100% create a separate job of class over here for this I'm just going to do it inside of here because I'm not planning on reusing this adapter All right, so I'm going to call my adapter what I what we're going to have to do is we're going to extend subclass this class and tell it How to render the items in a slightly different way? Okay, so let me define my inner class and I'll call it patient adapter Okay, and it is going to extend the array adapter Mm-hmm. Mm-hmm. Mm-hmm. It is going to freeze There we go Android studio it's going to extend the array adapter for the patient class Okay All right, so I have to give it a constructor, right? Which is what it's mad about right here. So if I do Control all shift enter all sorts of stuff, you know what? Let me just define it patient Adapter, okay, that's what the constructor is going to look like It's going to take a context. It's the first thing has to have that context We're going to need it. You'll see why in just a second has to have the context and it has to have The data that it wants to take in right so like this here, right? When we defined the adapter before we said you're going to be based off an array list of patients This adapter to is going to be based off an array list of patients And we'll call patients Okay so Now it's going to tell me well you need to call the super The super constructor class. So let's do that super Context zero patients Okay, so Sorry, I was just trying to figure out what I did I type patient adapter here that shouldn't be the case There we go. Now. We're all happy. Sorry about that. Okay, so patient adapter extends an array adapter of patient Okay, so here we go had to call the super constructor if you don't call the super constructor, you know the the root of this Things just won't be configured correctly and that's why we need this context All right, so we're going to pass in the patient's list just like was done here All right, so this constructs the thing now, but the big thing that Changes how we draw these items is we have to Override a method. Okay, so I'm going to that method. I'm going to hit control o or command o okay, and Now you get this big long list of select methods to override and implement holy cow The one we need to override and implement is called get view. So if I start typing get view, I get it here Okay, this is the one get view position in to convert view parent. Okay, blah, blah, blah All right, so that gives us the the method that we need to override now How do we do that? What do we need to do? Get view takes a few or it has a few parameters. I'm gonna make this big so we can see it Get view takes a few parameters. The first is the position. So we're in a list here, right? This is going to be give me the view here view means widget in Android give me the view for position say two of the list so Give me the view for position zero is going to be show me what you want to show me for this data Peter 9 9 Position 1 Peter 88. Okay, that's what position is convert view The list view is smart right everything in here each one of these is a view. It's a widget Right if a widget goes off the screen, it will reuse it It will recycle it a little bit kind of like the recycler view does right so if this thing If the second parameter is Not null it's saying I've already built the view just replace its data Finally the view group. We actually don't need to worry about that. We're not going to use it. Okay Except for well, no, that's not true. We might need to use it We need it in the constructor or not in the constructor, but when we create the the layout thing All right, let's just get into it shall we first of all delete this return statement. We don't want that We don't need it. We're going to make our own view. Okay first thing we want to do is we want to check to see If this convert view parameter is null Okay, if it's not null, we're just going to reuse it. This is saying I've already built a Thing here, and I'm going to show it to you. Okay, this says I've already built the layout All right, so what layout is it? I've actually already given that file to you So if you go into let's just take a preview of what we want to replace the list view with Go over here into your explorer Rests layout. There's one called patient list item And if you open it You'll see what this looks like. It's pretty simple. We're saying that for every patient trying to make this big Show a label called the name and then after here We're going to put the data and then show a label called the age and after here We'll put the age data and this layout file. It's a constraint layout. It's got text views. That's it. That's all it is You can feel free to have a look at it. There's nothing tricky in here. Nothing special. It's just a layout file All right, so let's go back to the activity If convert view is not Null it says I already have one of these things just change its data if it is null You need to build up a new one for me to display, right? So this will get called the first time you build up the list view Make me a new one of those things right, so What we'll do is well, we will Call the thing that will build Widgets from a layout file is called the layout inflator, right? And it's the same well The set content view on your activity up here on create also uses the layout inflator the layout inflator is baked into Android It's the thing that reads the XML file for a layout and converts it into Java widgets. Okay, so we're going to call Layout inflator dot from and now we got a Give it a context. Okay, so get context the context being me the Activity and then we call inflate and you give it the layout Okay, so we're in our dot layout and then the name of my layout is the name of this file over here Patient our dot layout dot patient list Item, okay And then there's two more parameters that go here parent and False okay, we don't have to worry about those so don't worry about the fact that this Thing here is is being screwy This is actually the correct Form of this it's just Android studio is kind of out of sync Don't worry about this redness right here It should go away if it doesn't go away. I've made a bug, but it should go away. This is a bug in Android studio It's just not happy right now All right, so this inflates the view it makes a widget for you to put data in Let's assign it to the convert view variable right convert view is null. Let's give it the widget we just made Okay Now what do we do? We want to put the data into the layout file All right, so I've got this layout file. I've got a name and an age over here I've got a label and then I've got the item name Which is going to be in the text view where I want the name to go and I've got an item age Which is the text view it's blank right now so you can see it Where I want the age to go so I need to get these text views out of the view I just made and set their data. Okay, so text view Patient name is Okay, we got a call convert view Right the view we just created find view by ID and find view by ID is going to search within the widget When you call find view by ID just by itself Like we did up here with the list view it says find it within me whoever the calling widget is Find it within me. This says find it within this widget. Okay, this widget is the convert view. I just made find view by ID the name of it is I think it's name What did I call it item name? Okay It's the name of the text view where I want to put it and then there's also the patient age item age All right, so this gives me the views now. I need to set their data Okay, but what data do I give them? All right, how do you figure out who goes where? well Recall just mentioned a few minutes ago this call to get view has a position parameter And this position corresponds to whichever item in the list you want to show. Well, where's the list? We passed it in to the constructor Okay, so this is the list that we're gonna set now If I need to update it, okay, or if I want to reference it I should probably put it in a field variable here. So let me do that and Then here I will in the constructor initialize it Let's stop patience gets patience. Okay All right now You want to show item number? 012 you want to show Bob 55? well The patience object is an array list so I can get the patient. I want to show by calling patience dot get The index of the item you want to show well the index of the item I want to show is this position parameter. So finally Then what we need to do is patient Now that we have the patient and the patient has the data in it, right? It's been built up from calls to the firestore now. We need to set the value in the text view so patient name and Then the call is going to be dot set text That sets the text content of a text view and what do we want to set it to? We want to get the patient's name out of this object P dot get name using my getter Whoops and Then for the age we want to set that to patient dot set text If you remember get age is going to return an integer. Okay, I should still do the conversion for us All right final thing final thing we have to do is return this view that we created Okay, that's called get view. We got to return the view that we just made Okay All right, so this is my adapter I've created the adapter that tells the list view how I wanted these things displayed and I want to display it in this new Layout I've created All right then so final thing is I need to Change up my adapter a little bit Okay, so my array adapter now is not gonna be the old Baked-in array adapter. It's gonna be this new patient adapter class I defined right and the patient adapter takes the context as the first parameter and then the list of patients Oh, but initially the list of patients is blank. Whoops. So let me give you that Blank All right, so initially it's blank, but it's hooked up and it's ready to display them now when we refresh Everything should actually still be okay. We shouldn't have to change anything down here because we have an array list of patients here The adapter is just saying how to display it. All right, let me run my app. Let's see if we've got anything good All right, it compiled. That's a good sign Launched the activity. That's a good thing despite the fact that Android studio is still mad at my list item. Let's refresh Ah crunch something bad happened. Okay thing crash. So let's take a look what happened change my fire store Gotta change my filter here to find out what went wrong. It's dead. So let me look at this guy. What happened? String resource ID fatal exception resource not found. Ah, it is actually mad at me about this Oh P dot set text dot get age. Ah, okay So the problem here The problem here is that get age is an integer and It's trying to set text on get age Well, that's not gonna work too well because get age is an integer and Android is unfortunately at this point not smart enough to Convert this integer to a string It's also the case that this Error message is not really indicative of that problem. Is it? Let's convert this into a string integer dot to string. Give it the string See if that fixes it Open our firestore list view refresh the data There we go Okay, so we can see here now. It is showing the items in my list view the way I Wanted them to be shown. Okay, and if I go ahead and add People here I Know I spelled that wrong submit them refresh There she is she's showing up. All right, so that's it Maybe a little bit different way and hopefully simpler way overall of interacting with the firestore You have lots of options here Good luck with it. Let me know if you have any questions