java - 更新 Firebase Android 上的用户个人资料
问题描述
我在更新用户个人资料时遇到问题。就我而言,我将更新用户的两个字段(位置和可用性)。它们的数据类型是布尔值。请帮助如何更新用户配置文件。我的方法如下:
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();
}
});
}
我的用户类是:
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;
}
}
解决方案
将以下方法添加到您的 User 类:
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;
}
}
并像这样更新您想要的任何道具:
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
}
});
在哪里
和在reference
哪里FirebaseDatabase.getInstance().getReference("Users");
user
FirebaseAuth.getInstance().getCurrentUser();
推荐阅读
- python - Pycharm设置问题。(对不起,我是新手。)
- hangfire - Hangfire 使用用户上下文运行后台作业
- ios - iOS 13.1 Objective-C ModalPresentation 全屏
- google-chrome - 更改行为 0.76 -> 0.77 // 始终在关联的应用程序中打开这些类型的链接
- node.js - node.js 中的 CORS 策略不更新标头
- python - 登录到抓取 - 获取真实性令牌但生成的 html 用于登录页面而不是目标页面 - 我做错了什么?
- transformation - 如何创建交叉表以填充在 SQL ORACLE 中创建的新列的值
- jquery - 使用添加按钮动态创建由下拉列表、文本框等组成的无限部分
- javascript - 尝试构建一个自动建议功能,该功能将接受或解析多个值,并且只接受以特定字符开头的值
- function - 如何检查 Azure Function 是否仍在运行