简体   繁体   English

更新 Firebase Android 上的用户配置文件

[英]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.它们的数据类型在 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:我的用户 class 是:

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;
    }
}

Add following method to your User class:将以下方法添加到您的用户 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");其中referenceFirebaseDatabase.getInstance().getReference("Users"); and user is FirebaseAuth.getInstance().getCurrentUser(); userFirebaseAuth.getInstance().getCurrentUser();

As the Firebase documentation on updating a user profile shows, you need to build a UserProfileChangeRequest .正如有关更新用户配置文件的 Firebase 文档所示,您需要构建一个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."); } } });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法更新 firebase android java 中的配置文件值 - Unable to update profile values in firebase android java 使用 AsyncTask 更新 Android 中的用户配置文件图像 - Using an AsyncTask to update a user profile image in Android 如何使用 firebase 和 Picasso 库更新用户个人资料图片 - how to update user profile image using firebase and Picasso library 更新 firebase Android 工作室中的用户个人资料图像 - Update user profileimage in firebase Android studio 用于检查Firebase数据库中是否存在用户的个人资料信息的Firebase Android方法 - Firebase Android method to check existence of user's profile info in Firebase database 如何使用Retrofit 2在Android上同时更新用户个人资料文本字段和个人资料照片 - How to update user Profile textfields and profile photo same time on Android Using Retrofit 2 如何使用 android Studio 在 firebase 的登录用户上添加用户的全名和头像 - How to add user's full name and profile picture on logined user of firebase using android Studio 在 android 中进行身份验证时,将用户个人资料图片上传到 firebase 时出错 - Having error while uploading user profile image to firebase, during authentication in android Firebase 多登录方法但单用户配置文件 - Firebase multiple login method but single user profile Firebase Android中的更新项目 - Update item in Firebase Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM