 Hey everyone, so I'm going to follow up the previous video on adding data into the Firestore with a super simple demonstration of how to get the data out of the Firestore, okay? So we're picking up right where we left off at the end of that adding to the Firestore video And now our goal is to get the data out of the Firestore to query the data and we'll Start out with something very simple What I'm going to do is I'm going to add another button here first of all so Did did do now. Let's make this big. All right I'm just going to add a button and we don't need the logcat right now. So let's shrink that I'm going to add a button just below the submit button And in fact, I'm going to copy and paste the submit button because it's going to kind of be pretty similar I'm going to call it Refresh, it'll be the refresh button It'll show the text Refresh, we're going to refresh the data We're going to call a different method when this button is clicked. It's called on Refresh click Where do we want it? We want it in the middle horizontally and let's put it below the submit button, okay? There we go, and I probably want to spell refresh correctly and let's give it a little margin up at the top Layout margin top just something small 12 and Now let's do it. It doesn't really matter, but let's give it a little bit of margin All right, so now I got a second button there and I need to define this on-click method Right, let me I'll expand this so you can see the whole thing for the button and You can pause here if you need to look at this for a second Okay, so I'm going to go back to my firestore list activity and let's handle What's what we want to do when somebody clicks on this button? Okay, so I'm in my firestore list activity And we're going to do public void and then the name in the on-click method which I copied on Refresh click With the view all right, so what do we want to do? We want to get the data out of the firestore How do we do that? It's actually not so bad Before we called mdb.collection.add this time we're going to call a slightly different method which is get okay, so let's Get our firebase instance object firestore mdb Once again, we have to specify the collection that we're dealing with here Before the collection where we put our data in was called patience, so we should probably Get our data out of patience as well Now here once again, I've got this string that I'm reusing in a couple of places Good idea to just make a constant out of it and reuse it right and then that way if for some reason We change the collection that we're using later We only have to change it in this one place up here in patience. Okay, so I've created this public final string patience and Instead of specifying it manually I'm gonna reuse the string here, and then I'll also use it down below in the submit click Okay, so collection up patience now What do we need to do if you want all of the objects in the collection? It's pretty simple you call get Okay, that gives you all of the objects in the collection Like add this is an asynchronous call right so it's going to go up to the cloud Ask for all the data and bring it back to you over the internet. So this is an asynchronous call It's your code does not stop at this point, right? You can't assign this to anything Won't work what you have to do like we did with add is call the Let's add a success listener. Okay so This success listener method will get fired off when the data comes back We have to make a new on success listener just like we did down below I'm going to use the autocomplete again to my advantage Okay, and it's in here in the body of this on success method where we want to do the work Okay, and actually go through the information that comes back and the information that comes back takes the form of this query snapshot class kind of weird stuff That's something particular to the fire store So but let's go through that data and see what we've got. Okay, so this is a collection Okay, it is something and as a collection. It is iterable so we can loop over this thing Okay, the way we loop over it. Let's create a for loop or for each loop the Types of data that is in this collection is query document snapshot Okay, so we've asked to give give me all the documents in this collection Okay, one document is a query document snapshot Okay, so call it document so for each query document snapshot in This guy that I'm getting back We can do something. Okay First let's just log it Okay, log it with the tag now the example that firebase provides is you can get Document id right this is or excuse me document get id Okay, this gives you the unique value associated with the document. So this this thing here these weird strings Okay document dot get id plus now in their example they have So arrow and then if you want the data you call document dot get data Okay, now let me kind of do get data now You can see here Android studio is telling you that get data returns a map right a map of strings and Objects for each document right those strings and objects are this is the field the string is the field name And the object is the value right so you're getting a map back from the fire store Okay Now if we print this out Or if we run our app, let's shrink this we can get our app up In our log cat, so I'm opening the log cat Probably got it down off the screen. Let's drag it up a little bit. Okay, so I'm opening the log cat here Let me rerun this to get these changes applied Mm-hmm Rebuild the whole thing Go into the firestore list view. Let me pull up again pull up your log cat Okay, now when I hit refresh what I should get is a list of all the data that was in there, right? That's what I told it to do For each document show me its data and there it is and it took a second right it took a second to populate this thing If we scroll over a little bit Here's the map right name equals this age equals that pretty cool now We are not querying or reducing the the type of things you can do if you go back to the video on the firestore search It tells you how to build a query to only get certain things like certain things where the name is equal to Lucas for example That's a good thing collections can get large, you know think of the collection of Photo names on snapchat. You don't want to be downloading everything every time and then filtering it It's much better to filter it during your query Then after okay, but I digress Remember how we created a patient class to kind of capture the essence of you know It's not just a map of name and age. It's it's representing a patient the Firestore provides a method for converting these things into a patient object. It's pretty easy You call document right document is you get one document. That's what we're looping over dot to object Okay, document dot to object and then you got to give it the class to convert it into okay patient dot class Okay, that was the name of our patient class that we created. All right, this returns a new patient Object and we should store that somewhere okay, so if Here is where the names of your attributes in the patient class really matter remember we've got no argument constructor We've got getters and setters and they have a very particular name this is This method call here takes advantage of those naming schemes. Okay, but this is all we call now We've got a patient object and now I can treat this thing not like a map But more intuitively as a model in my application Why don't I put all of these? It'll be useful later. Let's put these into a list right So let me make a new array list Okay, this array list is going to hold a list of patient objects and I'll call it patients and I'll initialize it to be empty Okay All I'm going to do here with this array list is I'm going to add Convert every document into a patient and then shove it into this array list And that'll be useful for me in the future, right? So I've created a new patient here Let me just go ahead and add the patient Patience dot add P right Patience dot add P and that'll put them all in there All right, so that should do it for us, right? Let's check and make sure that this worked What I'm going to do I'm going to take this log statement out and let's modify it whoops you know, let's take it out. Let's put it down here and Actually, let's move it here and instead of logging the contents of the document Let's log the patient that we just created P dot get name plus Put a little space P dot get age right So here we've converted it to an object a Patient object instead of logging the document directly now. I'm logging the patient object and Oops, I've got to apply my changes Okay, let me clear out my logcat. I'm going to click on the trash can here clear out the logcat. Let me refresh Alright, so now Here I'm logging This instead pretty cool. All right, so now I've got the data out nice I've got an array list of all the patients that are in the patient's collection The next step is I probably want to do something. I probably want to show that to the user And we'll go over how to do that in the next video