如何将多种类型的用户重定向到他们各自的活动? [英] How to redirect multiple types of users to their respective Activities?
问题描述
我正在Firebase上创建投票应用程序.我有3种类型的用户.到目前为止,在使用以下代码登录所有用户后,我可以成功将2种用户(学生,教师)重定向到他们各自的活动,到目前为止,我的用户数量 但是,现在我必须添加另一个用户(ADMIN),并且像其他用户一样,在登录后,管理员也应重定向到他们自己的特定活动.我对如何修改第三位用户的代码感到困惑.
I am creating a voting app on Firebase. I have 3 types of users. So far i can successfully redirect 2 kinds of users (STUDENTS, TEACHERS) to their respective activities after they login all with the code below, MY Users so far But now i have to add another user (ADMIN) and like other users, admin too should be redirected to their own specific activity after login. I am confused as to how to modify my code for a third user.
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(FirebaseAuth firebaseAuth) {
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
if (mAuth.getCurrentUser() != null) {
String uid = mAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
uidRef = rootRef.child("STUDENTS").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
//start students activity
startActivity(new Intent(MainActivity.this, student.class));
} else {
//start teachers activity
startActivity(new Intent(MainActivity.this, teacher.class));
}
}
//
@Override
public void onCancelled(DatabaseError databaseError)
{
}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);
}
else{
Log.d("TAG", "firebaseUser is null");
}
}
};
推荐答案
仅使用if (dataSnapshot.exists())
不能解决您的3种类型的用户问题.假设第三个用户的类型为3
,则需要更改数据库结构.因此,您的新数据库架构应如下所示:
Using onlye if (dataSnapshot.exists())
will not solve your 3 types of user problem. Assuming that the type of the third user is 3
, a change in your database structure is needed. So your new database schema should look like this:
Firebase-root
|
--- users
|
--- uidOne
| |
| --- name: "Ed"
| |
| --- type: 1
|
--- uidTwo
| |
| --- name: "Tyff"
| |
| --- type: 2
|
--- uidOne
|
--- name: "Admin"
|
--- type: 3
现在您应该在uid
节点上添加一个侦听器,并像这样检查用户的类型:
Now you shoud add a listener on the uid
node and check the type of the user like this:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("users").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.child("Type").getValue(Long.class) == 1) {
startActivity(new Intent(MainActivity.this, student.class));
} else if (dataSnapshot.child("TYPE").getValue(Long.class) == 2) {
startActivity(new Intent(MainActivity.this, teacher.class));
} else if (dataSnapshot.child("TYPE").getValue(Long.class) == 3) {
startActivity(new Intent(MainActivity.this, admin.class));
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage());
}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);
这篇关于如何将多种类型的用户重定向到他们各自的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!