如何针对不同类型的用户进行不同的活动

时间:2018-02-06 16:38:17

标签: android firebase android-intent firebase-authentication

我正在android studio& amp上开发一个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);

2 个答案:

答案 0 :(得分:0)

好的,所以代码中的主要问题是比较字符串。你不能使用&#34; ==&#34;同时比较字符串。使用comparesTo

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

答案 1 :(得分:0)

您的问题并未明确说明您的问题。

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

usertype.equals("Student")

如果字符串&#39; usertype&#39;它返回true。和#34;学生&#34;是平等的。 希望这会有所帮助。