Jovan
Jovan

Reputation: 306

How to update specific fields in Firebase JSON node

I'm working on Geo location Android application which is using Firebase for back end and database.

Here is my structure for the data in my database.enter image description here

Here is mu User.java class.

@IgnoreExtraProperties
public class User {

    public String fullName;
    public String email;
    public String longitude;
    public String latitude;

    public String getLongitude() {
        return longitude;
    }

    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }

    public String getLatitude() {
        return latitude;
    }

    public void setLatitude(String latitude) {
        this.latitude = latitude;
    }


    public User() {

    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

What I want to do.

How I'm doing it now?

What is wrong?

Upvotes: 0

Views: 1669

Answers (1)

Levi Moreira
Levi Moreira

Reputation: 12005

The problem is that you're not setting fullName and email when setting latitude and longitude, then those field are null. setValue() will erase the old object and add the new one with the fields null. You should be doing an updateChildren instead of setValue.

Map<String, Object> latLong = new HashMap<>();
latLong.put("latitude", "123");
latLong.put("longitude", "123");

dbRef.child("users").child(currentUserId).updateChildren(latLong).addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()) {
            Log.i("Info", "updateUser:success");
        } else {
            Log.i("Info", "updateUser:failure");
        }
            }
        });

You can keep using your code if you set the email and fullName alongside with latitude and longitude.

Upvotes: 1

Related Questions