如何根据Firebase信息重定向到其他页面
问题描述:
对于我的项目,我想允许用户单击带有Firebase的按钮(使用电子邮件和密码登录)后,根据其身份(例如:买方,卖方)登录各自的主页.我不确定完成的方式.
For my project, I would like to allow users to login to respective homepages based on their identity (eg: Buyer,Seller) after clicking a button with the firebase sign in with email and password method. I am unsure about the way to get it done.
这是我尝试过的方法,我被定向到买卖双方的SellerHomepage.
Here's what I tried and I was directed to SellerHomepage for both Buyers and Sellers.
fAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
db = FirebaseDatabase.getInstance().getReference("users");
db.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren() ) {
String userType = ds.child("type").getValue(String.class);
if (userType.equals("Buyer")) {
startActivity(new Intent(MainActivity.this,BuyerHomepage.class));
}
else if(userType.equals("Seller")) {
startActivity(new Intent(MainActivity.this,SellerHomepage.class));
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
else {
Toast.makeText(MainActivity.this,"Error !" +task.getException().getMessage(),Toast.LENGTH_SHORT).show();
}
}
});
答
@Danial检查此代码
@Danial check this code
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference("users");
rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot:snapshot.getChildren()){
String mail=dataSnapshot.child("type").getValue().toString();
if (userType.equals("Buyer")) {
startActivity(new Intent(MainActivity.this,BuyerHomepage.class));
}
if(userType.equals("Seller")) {
startActivity(new Intent(MainActivity.this,SellerHomepage.class));
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});