【问题标题】:How to intent to different activities for different types of users [duplicate]如何针对不同类型的用户进行不同的活动[重复]
【发布时间】:2018-07-16 19:10:39
【问题描述】:

我正在 android studio 和 Firebase 上开发一个 android 应用程序。 有 4 种不同类型的用户(学生、导师、家长、管理员)。 我想用这样的 if 语句来做:

 if (user.profile.userstatus == "Student")
     intent studentactivity
 if (user.profile.userstatus == "Tutor")
     intent tutoractivity

那么,我的主要问题是如何从 firebase 获取数据?

而且,这是我的区分代码:

if (isRegistering) {
    mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
        showProgress(false);

            if (!task.isSuccessful()) {
                Toast.makeText(LoginActivity.this, "Could Not Register", Toast.LENGTH_SHORT).show();
            } else {
                // show username diaglog
                UsernameDialogFragment dialog = new UsernameDialogFragment();
                dialog.show(getFragmentManager(),null);
            }
        }
    });
} else {
    Task<AuthResult> authResultTask = mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            showProgress(false);
            if (task.isSuccessful()) {
                // Sign in success, update UI with the signed-in user's information
                FirebaseUser user = mAuth.getCurrentUser();


                //student login
                if (get idstatus == "Student")
                Intent studentintent = new Intent(getBaseContext(), studentActivity.class);
                startActivity(studentintent);

                //admin login
                else if (get idstatus == "Admin")
                Intent adminintent = new Intent(getBaseContext(), AdminActivity.class);
                startActivity(adminintent);

                //parent login
                else if (get idstatus == "Parent")
                Intent parentintent = new Intent(getBaseContext(), ParentActivity.class);
                startActivity(parentintent);

                //tutor login
                else {
                Intent tutorintent = new Intent(getBaseContext(), TutorActivity.class);
                startActivity(tutorintent);
                }
            } else {
                // If sign in fails, display a message to the user.

                Toast.makeText(LoginActivity.this, "Authentication failed.",
                        Toast.LENGTH_SHORT).show();

            }

这是用于存储数据的代码(现在可以使用):

public void onClick(DialogInterface dialog, int id) {
    // sign in the user ...

    //https://myapplication4-124da.firebaseio.com/ https://console.firebase.google.com/project/myapplication4-124da/database/myapplication4-124da/data/

    EditText usernameField = ((AlertDialog) dialog).findViewById(R.id.username);
    EditText firstnameField = ((AlertDialog) dialog).findViewById(R.id.firstname);
    EditText lastnameField = ((AlertDialog) dialog).findViewById(R.id.lastname);
    String username = usernameField.getText().toString();
    String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
    String firstname = firstnameField.getText().toString();
    String lastname = lastnameField.getText().toString();

    User aUser = new User(username, firstname, lastname, choice, 0.0);

    FirebaseDatabase.getInstance().getReference("users").child(userId).child("profile").setValue(aUser);

    Intent intent = new Intent(getActivity().getBaseContext(), studentActivity.class);
    startActivity(intent);

【问题讨论】:

  • 你能解释一下你所面临的问题吗?
  • 我必须将不同类型的用户(idstatus;“student”、“tutor”、“parent”和“admin”)发送到不同的活动页面。所以,我必须从 firebase 读取保存的 idstatus 数据。而且我想使用if语句来区分idstatus,但我不明白如何从firebase读取用户类型数据。你能帮帮我吗?

标签: android firebase android-intent firebase-authentication


【解决方案1】:

好的,所以代码中的主要问题是比较字符串。比较字符串时不能使用“==”。使用comparesTo

例如 if(string1.comparesTo(string2)==0)//this means they are equal { Log.i(TAG,"Strings are equal");}

【讨论】:

  • 你能告诉我如何读取存储在 users.child(UId).child(profile).child(idstatus) 中的数据吗?这样我就可以将字符串与用户类型进行比较? (学生、管理员、导师和家长?)
【解决方案2】:

您的问题没有明确说明您的问题。

假设您遇到的唯一问题是将获取的数据中的值与字符串进行比较,“==”在字符串的情况下不起作用。你必须使用

usertype.equals("Student")

如果字符串 'usertype' 和 "Student" 相等,则返回 true。 希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多