这是我的数据库结构,我希望根据类型将用户重定向到他们各自的活动。我不知道逻辑或语法。 出于某种原因,似乎没有任何作用。
mAuthListener= new FirebaseAuth.AuthStateListener() {
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if (firebaseAuth.getCurrentUser() != null) {
startActivity(new Intent(login.this, MainActivity.class));
}
} };
cardView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startsignin();
}
});
}
@Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
private void startsignin()
{
String email = editText.getText().toString();
String password = editText2.getText().toString();
if (TextUtils.isEmpty(email) || TextUtils.isEmpty(password)){
Toast.makeText(login.this,"Some fields are empty",
Toast.LENGTH_LONG).show();
}
else
{ mAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(Task<AuthResult> task) {
if (!task.isSuccessful())
{
Toast.makeText(login.this,"Sign in failed", Toast.LENGTH_LONG).show();
}
}
});
}
}
答案 0 :(得分:0)
为解决此问题,建议您使用以下代码行:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("STUDENTS").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()) {
//start students activity
} else {
//start teachers activity
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage());
}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);