android - 如何使用 Firebase 创建用户个人资料页面?
问题描述
是否可以在不使用任何其他 SQL 数据库的情况下在 android native 中创建用户配置文件页面,因为我不擅长 JSON。
我想创建两个活动,一个将向登录用户(firebase auth)显示个人资料页面,而对于其他用户,第二个布局将用于编辑个人资料以及添加个人资料图片、全名、年龄和地址的能力(我使用了 Firebase 电话身份验证,因此无需使用电子邮件地址或密码)。
我试图获取用户 ID 并将其与其他属性一起存储到 firestore 的新集合中,但它对我不起作用。
解决方案
像这样在 Firestore 中创建文档
DocumentReference docRef = firebaseFirestore.collection(userId).document("Profile");
docRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
if (documentSnapshot.exists()) {
Log.v(TAG, "Profile exist");
getProfileData();
} else {
Log.v(TAG, "Profile is not exist");
firebaseFirestore.collection(userId).document("Profile").set(profileEntity);
}
}
});
如果配置文件不存在,它将创建一个新的。将数据存储在配置文件实体中并保存在配置文件文档中。
如果配置文件存在:
private void getProfileData() {
DocumentReference docRef = firebaseFirestore.collection(userId).document("Profile");
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document != null) {
ProfileEntity profileEntity = task.getResult().toObject(ProfileEntity.class);
if (profileEntity != null) {
//show your profile data
}
} else {
Log.d(TAG, "No such document");
}
} else {
Log.d(TAG, "get failed with ", task.getException());
}
}
});
}
推荐阅读
- html - 包装器 div 旁边的 div 之前的额外空间
- excel - NetSuite - 使用 SuiteScript 2.0 生成 Excel 在打开时不断损坏
- scala - 在恢复部分方法中调用未来
- python - 为什么在导入这些 PNG 文件时会出现错误?
- node.js - 如何在 sequelize 中的两个模型之间建立一对多关联?
- react-native - 添加新项目时防止倒置的平面列表滚动到底部
- discord - 我如何使用在 discord.js 机器人上使用提及的命令
- go - Go To Definition (Ctrl + Click) in VS Code v1.45.1 with Go v1.14.1 and Ubuntu 20.04 not working
- ngrx - Can I get data from an NgRx service without updating cache?
- javascript - How to create an object with a parameter