Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update specific node value on Firebase in android

I wan to update a specific node value on Firebase database. Lets say the new longitude value is 72.7665443 that I want to set in Firebase in place of longitude 25.

For this purpose, I need a key that is KZaouoZ8jRsZIYL_9Xg. How can I get the key of specific node? Is there an official method of updating the node?

enter image description here

like image 815
Humty Avatar asked Mar 23 '26 00:03

Humty


2 Answers

Generally in no-sql databases you get the concept of Key-Value pairs, in this case it is similar, it is a node with a value.

Please provide more details about the problem... You need the key to access the value, in this case UsersDatabase seems to be a container of User data, probably linked to a FirebaseAuth account, if that is the case then your user's UUID should match the key on the database, here is how to get it:

String userUid = FirebaseAuth.getInstance().getCurrentUser().getUid();

This scenario would be very simple, you get your database reference on the UsersDatabase node, and then you access to it's child object:

DatabaseReference databaseReference = FirebaseDatabase.getInstance()
        .getReference("UsersDatabase");
databaseReference.child(user.getUid()).setValue(/*YOUR OBJECT CLASS GOES HERE*/);

If not, then you can use alternatively an ad-hoc query to get the specific node by another field, for example:

DatabaseReference databaseReference = FirebaseDatabase.getInstance()
        .getReference("UsersDatabase");
Query filteredData = databaseReference
        .orderByChild("name")
        .startAt("Gulfad")
        .endAt("Gulfad");

Note: You should have a way to access the keys of those items. Querying items that way use to be hard for the performance of the database.

like image 74
kevinrodriguez-io Avatar answered Mar 25 '26 13:03

kevinrodriguez-io


When you're creating UsersDatabase you have to save that unique key in your sharedpreference class.

DatabaseReference databaseReference;
databaseReference=FirebaseDatabase.getInstance().getReference("UsersDatabase");
String uniqueKey=sharedprefrence.getUniqueKey();
Map<String, Object> taskMap = new HashMap<String, Object>();
taskMap.put("longitude", longitudeValue);
databaseReference.getReference("UsersDatabase").getChild(uniqueKey).updateChildren(taskMap);
like image 35
dinesh rathore Avatar answered Mar 25 '26 14:03

dinesh rathore



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!