‫Welcome back!
‫We learned how to read from and write to Firebase Real Time Database.
‫Now we're going to read and write custom objects to our real time databases.
‫I'll create a new Java class that will act as a custom object.
‫I'll name it as user.
‫And inside this user I'll create two variables string name and string.
‫Email Alt+ insert to create the constructor.
‫Having these two variables and I need to create getters and setters.
‫Then let's move to the main activity.
‫And here I need to comment this set value.
‫Create a new user user one new user and the name would be Jack and the email will be Jack at gmail.com.
‫And in the same way, to write a value to a specific database location, I'll use set value method,
‫my reference dot set value, and you can see that set value can accept an object as an argument user
‫one will be inserted, stop the application and run it again.
‫And there is an error because I don't need to use this and the reference messages will accept only value.
‫So I need to change the path.
‫The reference I need to create another reference.
‫I'll name it as users.
‫Let's run again and let's open our Firebase.
‫Once the application is created, you can see the users node is created, setting the name as Jack and
‫the email as Jack at gmail.com.
‫And the reason why our application is crashed because my reference is referring to this reference messages
‫and not users.
‫And if the new reference that contains custom objects will not be applicable by using this method.
‫So you use my reference dot add value event listener and you get the value whenever you have simple
‫values, simple data that are inserted in the database.
‫Whenever you have a key value, you use Snapchat dot.
‫Get value.
‫In this way, whenever you create and get custom objects like this, you need to use a different way.
‫So to read custom objects from nodes we have the reference is users, not messages to nodes, users
‫and messages.
‫I need to get the email in the users node.
‫Let's go up, check the references users and here I need to comment this because I don't need to set
‫the reference again, because it's set it and uploaded.
‫I need to read this custom object.
‫So how to read this custom object in the on data change method?
‫Similar to what we done before my reference dot add event listener method and inside on data change,
‫I need to create an object similar to what we insert into our database.
‫It's called user user user equal to snapshot here dot get value.
‫And what is the type of the custom object?
‫It's of type user dot class here get value before we specified string dot class.
‫Here I'm using user dot class referring to this custom object and custom reference.
‫Now I'll use this user object.
‫So I'm getting it from the snapshot.
‫I need to display it in the text view.
‫So text view dot set text.
‫And here email will be user dot get email.
‫Let's run.
‫And the app crashes.
‫Why.
‫If we go to the logcat we scroll down.
‫We can see that user doesn't define a no argument constructor.
‫Whenever you use Firebase in your applications, you should specify an empty constructor or no argument
‫constructor.
‫Click on constructor.
‫Select none and you can see the user constructor is created.
‫Let's run it again.
‫Here we go.
‫This is our application.
‫The text changed from Hello world to Jack at gmail.com.
‫We succeeded in reading and writing custom objects to Firebase Realtime databases.