首页 > 解决方案 > 如何使用 Firebase 创建用户个人资料页面?

问题描述

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

我想创建两个活动,一个将向登录用户(firebase auth)显示个人资料页面,而对于其他用户,第二个布局将用于编辑个人资料以及添加个人资料图片、全名、年龄和地址的能力(我使用了 Firebase 电话身份验证,因此无需使用电子邮件地址或密码)。

我试图获取用户 ID 并将其与其他属性一起存储到 firestore 的新集合中,但它对我不起作用。

标签: androidfirebasefirebase-authentication

解决方案


像这样在 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());
            }
        }
    });
}

推荐阅读