Insert and retrieve data from Firebase – Android

firebase_walkytechy_db

Insert and Retrieve data from Firebase real-time database

Continuing the Firebase tutorial series, in this post we are going to learn how to insert data into firebase database. We will also see how we can retrieve the data from firebase in real time.

I hope you already know how to integrate firebase in android. Because in this post, I am not going to get into the firebase integration part. If you don’t don’t know how to integrate firebase in Android, please see our Firebase registration tutorial. Where I have shown how to integrate firebase in android.

The database operation also needs the user to be authenticated first. We are actually sticking with the default security rules. Which requires users to be authenticated. So the user needs to be logged in before doing any insert operation or to retrieve data. For that, you can check our tutorial on Firebase login.

Okay so let’s see what we are going to do here. We are going to create a simple firebase project, which will have an activity. There will be one EditText for the user input, one TextView where will show the retrieved data, one Save button, and one Show button.

As we are working with firebase realtime database here, we have to add the firebase-database dependency in our app level build.gradle file.

Now let’s create the activity and name it FirebaseSaveRetrieve or anything you like and the layout will be activity_saveretrieve. I already created RegisterActivity and LoginActivity. If you haven’t already, please create the login and register activity first.

Note: If you don’t want to create the login and register activity, as of now you can change the default security rules.

Go to your firebase Database -> Rules and update the rules with the below rules so that anyone can read or write the database –

I will make a separate tutorial on firebase database rules.

So the only thing is left for us to do is the FirebaseSaveRetrieve activity.

Below is the layout XML for our activity_saveretrieve.

Now clicking on save button we have to insert the data into firebase database. To do this first we have to check if the user is logged in or not.

Note – Don’t check if the user is null or not if you have changed your database access rules to public.

We can check if the user is logged in or not, by checking if the current user is not null. If it is not null, then the user is logged in. We should always check this before doing any update or retrieve operation.

See the code below –

Now go to your Firebase console, open the project you have created. Go to Database and click GET STARTED.

firebase_android_realtime

Copy your database root URL. We need this URL to do our database operation. See the image below –

firebase_realtime_url

We have to get the reference to this URL so that we can insert data under it.

Now we will add a child key “my_text” and we will get the value of my_text from our EditText field. You can name the key anything you want.

To add a child we have a method called child(String childKey) of the DatabaseReference class. The child method accepts only one parameter, a String value which is the key. And we can set a value using the setValue(Object var1) method.

Below is the updated code of our FirebaseSaveRetrieve activity.

Firebase Database Insertion

Let’s run the app and see if the save operation is working. Enter something in the EditText field and click Save. If everything is good you will see a successful message.

To confirm if the data is inserted into your firebase database, go to firebase database and you will see the data you just saved.

firebase_walkytechy_db

You can see from the above image, we got our expected result. Here my_text is the key, and “Hey there, Hello from WalkyTechy” is the value.

Now we have to retrieve the value using the key “my_text”. Let’s do this –

To retrieve a data we can use addValueEventListener() or addListenerForSingleValueEvent() method. We can these valueEventListeners to listen for database changes.

Below is the code for our Show button. Here we are using addListenerForSingleValueEvent. There is a difference between the two methods addValueEventListener() and addListenerForSingleValueEvent(). I will make a separate article on that.

Firebase Database Retrieve

In the above code, we have checked if the child exists or not. If the condition satisfies, we can safely call getValue() on it.

Here is the complete code of our FirebaseSaveRetrieve activity.

That’s all. Run and see if everything is working or not. If you have any doubt or query related to this post, please comment below in the comment section.

 

You May Also Like

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *