gpt4 book ai didi

android - 如何使用 Firebase 创建用户个人资料页面?

转载 作者:行者123 更新时间:2023-12-05 00:21:06 25 4
gpt4 key购买 nike

是否可以在不使用任何其他 SQL 数据库的情况下在 android native 中创建用户配置文件页面,因为我不擅长 JSON。

我想创建两个 Activity ,一个将向登录用户(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());
}
}
});
}

关于android - 如何使用 Firebase 创建用户个人资料页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52218192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com