0

Trying to update a specific field of current information on firebase.

The closest I've gotten is https://www.programcreek.com/java-api-examples/?class=com.firebase.client.Firebase&method=updateChildren

But I can't seem to figure it out.

package com.example.updatefirebase;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private DatabaseReference databaseReference;
    private FirebaseDatabase firebaseDatabase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Connects to my database.
        databaseReference = FirebaseDatabase.getInstance().getReference("users");

        HashMap<String, String> userMap = new HashMap<>();
        userMap.put("name", "lol");
        userMap.put("image", "321321");
        userMap.put("image_thumbnail", "default");


        //Trying to write to root file.
       // databaseReference.setValue(userMap);

        Map<String, Object> mapaCompleto = new HashMap<>();
        mapaCompleto.put(lol, userMap);


        //Update firebase.
        databaseReference = FirebaseDatabase.getInstance().getReference("users").updateChildren(mapaCompleto);

    }
}

What I want to be able to do, is the outcomment my databaseReference.setValue(userMap);

And then update a specific place in my database. Like the name, from lol to joey, or whatever.

2 Answers 2

0

You can use updateChildren():

HashMap<String, Object> userMap = new HashMap<>();
userMap.put("name", "joey");
userMap.put("image", "321321");
userMap.put("image_thumbnail", "default");

databaseReference.updateChildren(userMap);

updateChildren() will update a specific field without overwriting other child nodes.

You can find more info in the following link:

https://firebase.google.com/docs/database/android/read-and-write#update_specific_fields

1
  • Found out, it was easier to use: FirebaseDatabase.getInstance().getReference("users").child("name").setValue("joey"); Oct 21, 2019 at 19:53
-1

Found out it was easier just to write:

FirebaseDatabase.getInstance().getReference("users").child("name").setValue("joey");

Because I am not going to have the information put in place, given users will eventually fill out my database with their own stuff.

I'd probably have to set it up with a getter setter method otherwise.

But for single information changes, this is by far the easiest, as I do know the path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.