Android Studio-Firebase onComplete即将致电

时间:2018-07-12 13:36:59

标签: android firebase firebase-realtime-database firebase-authentication

我不到一个月就开始使用android studio,所以我对此很陌生。

我正在使用Firebase身份验证和Firebase数据库登录系统。我的想法是完成登录并确认后,我将用户发送到两个活动之一,并检查将哪个活动发送给我,以检查他们是否已填写测验(女巫是其数据库中的布尔值)。 / p>

我的问题是我的onComplete方法首先运行,而不是我的quizChecked。多亏了我的日志打印,我知道我得到了正确的值,但是由于onComplete首先运行,因此我的布尔值始终为false ...

在使用onComplete方法之后,如何使quizResposta开火?

谢谢!

这是我在此类课程中的主要方法:

 private boolean quizChecked() {

    final FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference();

    myRef.child("user: " + mAuth.getCurrentUser().getUid()).child("quizChecked")
            .addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot snapshot) {

                    if(snapshot.exists()) {
                        boolean a = snapshot.getValue(boolean.class);
                        if (a == true) { // if quiz is done

                            quizChecked = (boolean) snapshot.getValue();
                            Log.e("TAG", "here true: " + quizChecked);


                        } else {// if quiz not done
                            quizChecked = (boolean) snapshot.getValue();
                            Log.e("TAG", "here false or null: " + quizChecked);
                        }

                        Log.e("TAG", "AFTER quizChecked: " + quizChecked);
                    }else{
                        Toast.makeText(Activity_Login.this, "No User!", Toast.LENGTH_SHORT).show();
                    }


                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    Log.e("onCancelled", " cancelled");
                }

            });

    Log.e("TAG", "________: " + quizChecked);

    return quizChecked;
}


private void loginVerification(String emailString, String passwordString) {

    mAuth.signInWithEmailAndPassword(emailString, passwordString).addOnCompleteListener(
            this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {

                    if (task.isSuccessful()) {// if quiz answered -> MainActivity

                        quizChecked = quizChecked();

                        if (quizChecked == true) {

                            Log.e("TAG", "here true in login: " + quizChecked);

                            Toast.makeText(Activity_Login.this, "Login Sucess",
                                    Toast.LENGTH_SHORT).show();

                            Intent intent = new Intent(Activity_Login.this, MainActivity.class);
                            startActivity(intent);
                            finish();

                        } else { // if quiz not answered ->  activity_quiz

                            Log.e("TAG", "here false or null in login: " + quizChecked);

                            Toast.makeText(Activity_Login.this, "Login Sucess",
                                    Toast.LENGTH_SHORT).show();

                            Intent intent = new Intent(Activity_Login.this, Activity_Questionario.class);
                            startActivity(intent);
                            finish();

                        }
                        Log.e("TAG", "AFTER login: " + quizChecked);

                    } else {
                        Toast.makeText(Activity_Login.this, "Login Failure",
                                Toast.LENGTH_SHORT).show();
                    }
                }
            });

}

0 个答案:

没有答案