"일꾼이 일을 잘하려면 먼저 도구를 갈고 닦아야 한다." - 공자, 『논어』.
첫 장 > 프로그램 작성 > How to Redirect Multiple User Types (Students, Teachers, and Admins) to Their Respective Activities in a Firebase App?

How to Redirect Multiple User Types (Students, Teachers, and Admins) to Their Respective Activities in a Firebase App?

2025-03-26에 게시되었습니다
검색:618

How to Redirect Multiple User Types (Students, Teachers, and Admins) to Their Respective Activities in a Firebase App?

Red: How to Redirect Multiple User Types to Respective Activities

Understanding the Problem

In a Firebase-based voting app with three distinct user types (students, teachers, and admins), there's a need to redirect each user to their specific activity after login. The current code successfully manages the redirection for two user types, but faces challenges when attempting to incorporate the third type (admin).

Modifying the Solution for Multiple User Types

To accommodate the third user type, the first step is to modify the database structure. The original schema only catered to two types of users. By introducing a third type, the structure must be updated to include a "type" field for each user. The revised database schema might appear as follows:

Firebase-root
    |
    --- users
          |
          --- uidOne
          |     |
          |     --- name: "Ed"
          |     |
          |     --- type: 1
          |
          --- uidTwo
          |     |
          |     --- name: "Tyff"
          |     |
          |     --- type: 2
          |
          --- uidThree
                |
                --- name: "Admin"
                |
                --- type: 3

Updating the Code

With the updated database structure in place, the code can be adjusted to handle the third user type. The following code snippet demonstrates how to retrieve the user's "type" value and redirect them accordingly:

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);

By implementing these changes, the voting app can now successfully redirect all three types of users (students, teachers, and admins) to their respective activities after login.

최신 튜토리얼 더>

부인 성명: 제공된 모든 리소스는 부분적으로 인터넷에서 가져온 것입니다. 귀하의 저작권이나 기타 권리 및 이익이 침해된 경우 자세한 이유를 설명하고 저작권 또는 권리 및 이익에 대한 증거를 제공한 후 이메일([email protected])로 보내주십시오. 최대한 빨리 처리해 드리겠습니다.

Copyright© 2022 湘ICP备2022001581号-3