-2

I am developing an android app on android studio & Firebase. There are 4 different types of users (student, tutor, parent, admin). I want to do it with if statements something like this:

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

So, my main question is that how can I bring the data from firebase?

And, this is my code for distinguishing:

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

            }

And this is code for storing data(now working):

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);
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Can u explain more about the problem you face? – Sivakumar S Feb 06 '18 at 16:59
  • I have to send different types of users (idstatus; "student", "tutor", "parent", and "admin") to different activity pages. So, I have to read the saved idstatus data from firebase. And I want to use if statement in order to distinguish the idstatus, but I don't understand how to read the user type data from firebase. Can you please help me? – HeeJoong Kim Feb 07 '18 at 19:35

2 Answers2

0

Ok, so the main problem in your code is in comparing strings. You can't use "==" whilst comparing strings. Use comparesTo

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

Behl
  • 129
  • 10
  • can you please let me know how to read the data stored in users.child(UId).child(profile).child(idstatus) ?? So that I can compare the string to usertypes? (Student , admin, tutor, and parent?) – HeeJoong Kim Feb 07 '18 at 15:55
0

Your question doesn't clearly state your problem.

Assuming that the only problem you're getting is comparing the value from fetched data to a string, "==" doesn't work in case of strings. You must use

usertype.equals("Student")

It returns true if the strings 'usertype' and "Student" are equal. Hope this helps.

Sandecoder
  • 154
  • 1
  • 7