‫Welcome back.
‫In this video we'll get the data from Firebase and display them into the recycler view of our application.
‫So here I need to get a database reference and Firebase database.
‫And this is what we learned before.
‫And in the Oncreate method we need to initialize this objects.
‫Database equals to Firebase database dot get instance.
‫And here the reference database reference equals to database dot get reference.
‫Here I need to specify the reference the node.
‫Let's go to the console of Firebase.
‫And this is our real time database I click here on this plus sign.
‫And I'll add a node called users.
‫Don't specify a value here.
‫Just click this plus sign to create nested key value pairs.
‫Here I'll specify a user number one.
‫Again guys don't specify any value.
‫Click plus sign.
‫And here the key value pairs that I'll mention for every user.
‫Please pay attention.
‫I'm getting custom objects from the firebase.
‫They should match my modal class, otherwise I'll need to use Json or mochi.
‫These are Json converters and used for serialization and Deserialization.
‫And I talked about these in the previous sections, and especially in the Retrofit Plus mVVM section.
‫But in order to make everything simple and run smoothly, I'll copy the fields and paste here.
‫In the key here I specify the first name would be Jack.
‫Hit enter and you can see the username of the first user.
‫One inside the users node is Jack.
‫And here I need to add the second key which is the phone number.
‫Copy it and paste it.
‫The value would be 123456789.
‫Hit enter.
‫You can see the user one has two fields phone number and username.
‫Again guys I'll press this plus sign.
‫Copy the group user and paste it here.
‫And friends this is the first user.
‫I'll create another user.
‫So I'll go up in the users node.
‫User number two don't press add.
‫Press the plus sign to create nested things.
‫Again guys, you should specify exactly the same keys for all the users in order to get them correctly
‫when fetching in Android applications.
‫So again guys, for every item you should specify the user name exactly and phone number exactly like
‫this and the group user exactly like this.
‫The user name number two is Ali.
‫I click add and here in the user number two I'll create phone number.
‫The value would be 88776655.
‫Enter.
‫And the group user again guys it's case sensitive.
‫This is from the work group.
‫User work I'll add another user.
‫Our database contains one node called users, and inside this node we have three nested nodes user one,
‫user two, and user three, and key value pairs.
‫Three key value pairs for each user.
‫Please guys pay attention.
‫These keys must match all the users, all the nodes in order to fetch them correctly in Android Studio.
‫Now let's move to the main activity and here I need to specify this node users and please pay attention.
‫It's case sensitive.
‫And here I'll scroll down and before notifying dataset changed, I need to fetch the data from Firebase
‫into RecyclerView.
‫So please pay attention.
‫I'll use database reference dot add value.
‫Event listener new value.
‫Event listener.
‫Add value.
‫Event listener is a method used to attach an event listener to a specific location in the database.
‫This event listener monitors changes at the specified database reference.
‫In this case, it's at the users node and allows you to respond to those changes in real time.
‫It's a fundamental mechanism for building real time applications with Firebase, and to listen for changes
‫at the specified location.
‫You call the Add Value Event listener method on the database reference and pass in an instance of value
‫event listener.
‫The value event listener is an interface with two key callback methods on data change and on cancel
‫on data change.
‫This method is called when the data at the specified location changes.
‫The data.
‫Snapshot parameter contains a snapshot of the data at the location on canceled.
‫This is a method called.
‫If there is an error while trying to read from the database, and you can use this method to handle
‫database errors.
‫We are interested in the on data changed method.
‫Here we need to get the data and get the list of users in the database.
‫So for that I'll use a for each loop data snapshot object.
‫And here I name it as data snapshot object.
‫And to get the data from here we need to use snapshot dot get children.
‫This is a for.
‫Each loop is commonly used in Firebase real time database to iterate through the child nodes or children
‫of a specific snapshot.
‫Snapshot in Firebase real time database represent a specific point in the database at a particular location.
‫It contains the data and structure of the database at the location at the moment the snapshot was created.
‫Get children method is used and called on at Data Snapshot object and returns an iterable interface
‫containing all immediate child nodes of that snapshot and the for loop in your code here iterates through
‫these child nodes one by one.
‫For each child node, the data snapshot variable represents the current child node within the loop iteration.
‫Then I need to display and get the items from the data snapshot object that we created here and here.
‫I need to start by creating an object user user equal data snapshot dot get value.
‫And what is the type of the value that we are getting?
‫It's of type user class.
‫So here I need to specify user dot class.
‫Make sure guys that you include a no arguments constructor in your model class in order to use it with
‫Firebase list users.
‫Dot add this user for every user inside the snapshot.
‫Go and add it to the users array list in our main activity.
‫Then I need to use this notify data set.
‫Changed code go up and after the loop notify the data set changed.