HashMap key as path – forward slash – Firebase – Android

multi_child_firebase

Multiple child keys using forward slash – HashMap

In my previous article, I have shown you how to insert multiple values into your firebase database. Where you have seen how to insert multiple values under a child key using HashMap. A HashMap can be used to store Kye value pairs. And you know that firebase also stores data in key and value pairs.

So using HashMap we can easily create a database structure like this –

firebase_hashmap_path

To create the above structure we can do something like this –

But suppose we want to add another child “starring” and its values will be another some key-value pairs.

multi_child_firebase

What would you do? Well, you could do the hard way or the not recommended way, like take the reference path of the child named “starring” and create children under it. But that will arise many other problems and you will end up doing a lot more code.

Instead, we can make the HashMap key a path, which is the recommended way of doing this. So let’s see how to do this by creating a simple project.

By default, the firebase database rules are not public. If you don’t want to authenticate the user, you can make the firebase database rule public. So that it can be accessed by anyone.

I am currently not changing the rules as I am authenticating the user and already created a Register and a Login activity.

Now let’s create an activity and name it KeyPathActivity and a layout activity_keypath. For simplicity, I am making this activity as simple as possible and making the key and values of the HashMap static.

Create the activity and create a Button widget. Clicking the button, the keys and the values will be inserted in our firebase database.

Here is the layout file of our activity_keypath

Get the reference to the button in your activity. We also need the reference to the firebase database. And if your firebase database rule is private or needs authentication, don’t forget to check if the FirebaseAuth.getInstance().getCurrentUser() is null or not.
If you are not doing any authentication and your database can be accessed by anyone, then you don’t need to check if the FirebaseAuth.getInstance().getCurrentUser() is null or not.

Below is the code for our HashMap object –

* Replace the getReferenceFromUrl(“#url”) with yours.

Now same as the composer, creator, and genre keys, we will create another key “starring”. And the value of the starring key will be some key-value pairs. To achieve this, we will concatenate the forward slash (/) with the String keys. See the code below –

Notice how I used the forward slash character (/).

Now to insert the data into your firebase database, use the updateChildren method. And at last, use the addOnCompleteListener and show the user if the values are successfully inserted into the database.

Below is the complete code of our KeyPathActivity

That’s all. Now run your project and hit the insert button. To confirm if the data is inserted or not, login to your firebase console.  Select database from the left navigation panel.

If you have any doubt or query or suggestion regarding this article, please don’t hesitate to let us know in the comment section.

 

You May Also Like

Leave a Reply

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