Java 如何在android中从FireStore检索用户数据

Java 如何在android中从FireStore检索用户数据,java,android,Java,Android,我想在用户签名完成后获得该用户的状态,并根据该状态向他显示其活动(如果他是管理员),然后向他显示管理活动(如果他是普通用户),向他显示用户活动 private void LoginUser(String email,String password){ fAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>(

我想在用户签名完成后获得该用户的状态,并根据该状态向他显示其活动(如果他是管理员),然后向他显示管理活动(如果他是普通用户),向他显示用户活动

    private void LoginUser(String email,String password){
        fAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if(task.isSuccessful()){
                    final DocumentReference documentReference = fStore.collection("users").document(fAuth.getCurrentUser().getUid());
                    documentReference.addSnapshotListener(new EventListener<DocumentSnapshot>() {
                        @Override
                        public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {

                                 if(documentSnapshot.getString("Status").equals("user")){

                                     Toast.makeText(LoginActivity.this, "Login Successfully", Toast.LENGTH_SHORT).show();
//   startActivity(new Intent(getApplicationContext(),HomeActivity.class));
                                     Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
                                     startActivity(intent);
                                     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                     startActivity(intent);
                                 }else if(documentSnapshot.getString("Status").equals("Admin")){

                                     //Then move to the AdminActivity
                                     Toast.makeText(LoginActivity.this, "he is a Admin", Toast.LENGTH_SHORT).show();
                                 }else{
                                     //throw error
                                 }

                        }
                    });


                }else{
                    Toast.makeText(LoginActivity.this, "Error "+ Objects.requireNonNull(task.getException()).getMessage(), Toast.LENGTH_SHORT).show();
                }
                loadingBar.dismiss();
            }
        });
    }
private void登录用户(字符串电子邮件、字符串密码){
fAuth.使用email和password(电子邮件,密码)登录。添加完整列表(新的完整列表(){
@RequiresApi(api=Build.VERSION\u CODES.KITKAT)
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
final DocumentReference=fStore.collection(“用户”).document(fAuth.getCurrentUser().getUid());
documentReference.addSnapshotListener(新的EventListener(){
@凌驾
public void OneEvent(@Nullable DocumentSnapshot DocumentSnapshot,@Nullable FirebaseFirestoreException e){
if(documentSnapshot.getString(“状态”).equals(“用户”)){
Toast.makeText(LoginActivity.this,“登录成功”,Toast.LENGTH_SHORT.show();
//startActivity(新意图(getApplicationContext(),HomeActivity.class));
Intent Intent=新的Intent(getApplicationContext(),HomeActivity.class);
星触觉(意向);
intent.addFlags(intent.FLAG_活动_新任务| intent.FLAG_活动_清除任务);
星触觉(意向);
}else if(documentSnapshot.getString(“Status”).equals(“Admin”)){
//然后转到AdminActivity
Toast.makeText(LoginActivity.this,“他是管理员”,Toast.LENGTH_SHORT.show();
}否则{
//抛出错误
}
}
});
}否则{
Toast.makeText(LoginActivity.this,“Error”+Objects.requirennull(task.getException()).getMessage(),Toast.LENGTH_SHORT.show();
}
loadingBar.disclose();
}
});
}

我建议创建一个类,它表示从数据库返回的值,我不确定Firebase DB中的命名约定或结构,因此下面是一个示例

科特林:

class User {
    val name: String? = null
    val status: String? = null
}
val documentModel = documentSnapshot.document.toObject(User::class.java))

if(documentModel.status == "user"{
 //do your work
}
爪哇:

然后在
onEvent
中,您应该能够执行以下操作:

科特林:

class User {
    val name: String? = null
    val status: String? = null
}
val documentModel = documentSnapshot.document.toObject(User::class.java))

if(documentModel.status == "user"{
 //do your work
}
爪哇:

您可能不需要执行
.getDocument
,您可能只需要执行
documentSnapshot.toObject
,但由于我不在IDE附近,我不得不猜测

我还建议在执行上述操作之前,确保快照不为空,以便您可以相应地进行处理

比如:

if (!documentSnapshots.isEmpty()) {
  // blah blah
}
聚苯乙烯;您的问题不是很清楚,我不确定您是否正在获取值,但是您在
onEvent
内部处理方面有问题,或者整个方法没有按预期工作,并且您当前没有检索任何值。不管怎样,我希望以上这些都能为你指明正确的方向