Alibek Shohnazarov
Alibek Shohnazarov

Reputation: 25

Update user profile on Firebase Android

I'm having a problem with updating the user profile. In my case I am going to update two fields of the user (location and availability). The data type of them is in boolean. Please help with how to update the user profile. My way is following:

private void updateProfile() {
    final Boolean isAvailable = swIsAvailable.isChecked();
    final Integer spinnerIndex = spLocation.getSelectedItemPosition();
    final Boolean location;


    if(spinnerIndex == 0){
        location = false;
    }else {
        location =true;
    }

    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    User updateUser = new User();

    updateUser.isAvailable = isAvailable;
    updateUser.where = location;
    
    user.updateProfile(updateUser).addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {
            Toast.makeText(UpdateActivity.this, "User Updated", Toast.LENGTH_SHORT).show();
        }
    });
}

My user class is:

public class User {
    public String fullName, phoneNumber, carName, email, uriImage;
    public Boolean isAvailable, where;

    public User(){

    }

    public String getFullName() {
        return fullName;
    }

    public User(String fullName, String phoneNumber, String carName, String email, Boolean isAvailable, Boolean where, String uriImage){
        this.fullName = fullName;
        this.phoneNumber = phoneNumber;
        this.carName = carName;
        this.email = email;
        this.isAvailable = isAvailable;
        this.where = where;
        this.uriImage = uriImage;
    }
}

Upvotes: 1

Views: 2172

Answers (2)

nodir.dev
nodir.dev

Reputation: 502

Add following method to your User class:

public class User {
    // ... your class props and constructors here

    // add this method
    @Exclude
    public Map<String, Object> toMap() {
        HashMap<String, Object> result = new HashMap<>();
        if(fullName != null)
            result.put("fullName", fullName);
        if(phoneNumber != null)
            result.put("phoneNumber", phoneNumber);
        if(carName != null)
            result.put("carName", carName);
        if(email != null)
            result.put("email", email);
        if(isAvailable != null)
            result.put("isAvailable", isAvailable);
        if(where != null)
            result.put("where", where);
        if(uriImage != null)
            result.put("uriImage", uriImage);

        return result;
    }
}

And update any prop you want like so:

User u = new User();

u.isAvailable = false; // new values go here
u.where = true; // new values go here

Map<String, Object> newUserValues = u.toMap();

reference.child(user.getUid()).updateChildren(newUserValues).addOnSuccessListener(new OnSuccessListener<Void>() {
    @Override
    public void onSuccess(Void aVoid) {
        // successfully updated
        
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {
        // failed to update
        // handle error here
    }
});

Where reference is FirebaseDatabase.getInstance().getReference("Users"); and user is FirebaseAuth.getInstance().getCurrentUser();

Upvotes: 2

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

As the Firebase documentation on updating a user profile shows, you need to build a UserProfileChangeRequest. From the docs:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
       .setDisplayName("Jane Q. User")
       .setPhotoUri(Uri.parse("https://example.com/jane-q-user/profile.jpg"))
       .build();

user.updateProfile(profileUpdates)
       .addOnCompleteListener(new OnCompleteListener<Void>() {
           @Override
           public void onComplete(@NonNull Task<Void> task) {
               if (task.isSuccessful()) {
                   Log.d(TAG, "User profile updated.");
               }
           }
       });

Upvotes: 0

Related Questions