1
votes

My Database in firebase is in this format

I need that if a user login then for that particular UID I need his associated department name. So How to take the department name as a String.I use this code to fetch department name

 String u_id=auth.getCurrentUser().getUid();
 mdatabase=FirebaseDatabase.getInstance().getReference().child("Users").child(u_id).child("department");
                         user=mdatabase.getKey();

By this i don't get the result.Please provide solution

public class LoginPage extends AppCompatActivity {
private Button btnLogin;
private TextView ForgetText;
private EditText userText,PassText;
private String UserEmail,UserPassword;
private FirebaseAuth auth;
private FirebaseAuth.AuthStateListener mAuthlistener;
private ProgressBar progressBar;
private DatabaseReference mdatabase;
public String departmental;
//private Spinner dropdown;
Variables v=new Variables();
private String username,user;
private Intent i;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login_page);


    auth=FirebaseAuth.getInstance();
    btnLogin=(Button)findViewById(R.id.btn_login);
    ForgetText=(TextView)findViewById(R.id.textView3);
    userText=(EditText)findViewById(R.id.email2);
    PassText=(EditText)findViewById(R.id.password2);
    progressBar=(ProgressBar)findViewById(R.id.progressBar2);

    btnLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            SignIn();
        }
    });

    ForgetText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(LoginPage.this,ForgotPassword.class));
        }
    });
}

public void SignIn(){

   UserEmail=userText.getText().toString().trim();
    UserPassword=PassText.getText().toString().trim();
if (UserEmail.isEmpty()){
Toast.makeText(LoginPage.this,"Please Enter the Email 
Id",Toast.LENGTH_LONG).show();
}
else if (UserPassword.isEmpty())
{
Toast.makeText(LoginPage.this,"Please enter Valid 
Password",Toast.LENGTH_LONG).show();
}
else {
auth.signInWithEmailAndPassword(UserEmail, UserPassword)
        .addOnCompleteListener(LoginPage.this, new 
OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                // If sign in fails, display a message to the user. If 
sign in succeeds
                // the auth state listener will be notified and logic 
to handle the
                // signed in user can be handled in the listener.
                if (!task.isSuccessful()) {
                    // there was an error

                    Toast.makeText(LoginPage.this,"Error in 
 logging!!",Toast.LENGTH_LONG).show();
                } else 

{ if(FirebaseAuth.getInstance().getCurrentUser().getEmail().equals(v.admin)) startActivity(new Intent(LoginPage.this,AdminUser.class)); else { String u_id = auth.getInstance().getCurrentUser().getUid();

                       mdatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(u_id).child("department");
                        ValueEventListener eventListener = new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                String  department = (String) dataSnapshot.getValue();

                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {}
                        };
                        mdatabase.addListenerForSingleValueEvent(eventListener);
                        Toast.makeText(LoginPage.this,u_id,Toast.LENGTH_LONG).show();
                        Toast.makeText(LoginPage.this,departmental,Toast.LENGTH_LONG).show();
                        /*i = new Intent(LoginPage.this, LoggedIn.class);
                        i.putExtra("hello_user",department);
                        startActivity(i);*/
                    }

                    Toast.makeText(LoginPage.this, "Logged In", Toast.LENGTH_LONG).show();
                   }
            }
        });

}

}
@Override
public void onBackPressed() {
    super.onBackPressed();
}

}

1

1 Answers

1
votes

Please use this code:

String u_id = auth.getCurrentUser().getUid();
mdatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(u_id).child("department");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String department = (String) dataSnapshot.getValue();
        Log.d("TAG", department);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
mdatabase.addListenerForSingleValueEvent(eventListener);

Hope it helps.