Ad

Direct To Each Activity According To Type Of User

I have created a project which has two types of user (patient and doctor). During login, I need to retrieve the role attribute in the firebase which under the users table. In my database structure, user type maybe is "doctor" or "patient". During login, I think I need to retrieve the role information and then assign them to different activity in android studio. However, my code seems doesn't work. The application keeps stopped. Is there anyone can help me. Thanks in advance.

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        email = findViewById(R.id.email);
        psd = findViewById(R.id.psd);

    }

    public void clicked(View v) {
        switch (v.getId()) {
            case R.id.login:
                LoginUser();
                break;
            case R.id.register:
     Intent intent = new Intent(this, ChooseRole.class);
                startActivity(intent);
                break;
        }
    }

    public void LoginUser() {
        String email1 = email.getText().toString().trim();
        String psd1 = psd.getText().toString().trim();

        mAuth.signInWithEmailAndPassword(email1, psd1)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            indentify();
                            finish();

                        } else {
                            Toast.makeText(Login.this, "couldn't login",
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    public void indentify() {
        String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
        FirebaseDatabase.getInstance().getReference(uid).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                long points = dataSnapshot.child("role").getValue(Long.class);
                String role = String.valueOf(points);
                if(role=="patient"){
                    Intent intent = new Intent(Login.this, HomePage.class);
                    startActivity(intent);
                }
                else{
                    Intent intent = new Intent(Login.this, HomePage2.class);
                    startActivity(intent);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                throw databaseError.toException();
            }
        });

    }
}

Hi guys, I changed it like this and it is successful. Thanks for everyone helping :D

 public void LoginUser(){
        String email1 = email.getText().toString().trim();
        String psd1 = psd.getText().toString().trim();

        mAuth.signInWithEmailAndPassword(email1, psd1)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()){
                            currentUser = mAuth.getCurrentUser();


                            String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
                            FirebaseDatabase.getInstance().getReference("users").child(uid).addListenerForSingleValueEvent(new ValueEventListener() {

                                @Override
                                public void onDataChange(DataSnapshot dataSnapshot) {
                                    if(dataSnapshot.child("role").getValue(String.class).equals("patient")) {
                                        startActivity(new Intent(Login.this, HomePage.class));
                                        return;
                                    }
                                    if (dataSnapshot.child("role").getValue(String.class).equals("doctor")) {
                                        startActivity(new Intent(Login.this, HomePage2.class));
                                        return;
                                    }
                                }

                                @Override
                                public void onCancelled(DatabaseError databaseError) {
                                    throw databaseError.toException();
                                }
                            });

                        }else {
                            Toast.makeText(Login.this, "couldn't login",
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }
Ad

Answer

Since in your database you have a node called users, then add a reference to that node, for example:

 FirebaseDatabase.getInstance().getReference("users").child(uid).addListenerForSingleValueEvent(new ValueEventListener() {
  @Override
public void onDataChange(DataSnapshot dataSnapshot) {

Then do the following:

Change this:

if(role=="patient"){

into this:

if(role.equals("patient")){

You need to use equals() when you want to compare the value of each object.

Ad
source: stackoverflow.com
Ad