简体   繁体   中英

Retrieving Specific Data from Firebase Realtime Database

Please help i want to retrieve a specific data from the Firebase Realtime database I have 2 users Professor and Student and i want to use that data for validations.

FireBase Realtime Database

在此处输入图片说明

 firebaseAuth.signInWithEmailAndPassword(email,pass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                progressDialog.dismiss();
                if(task.isSuccessful()){

                    //The task is successful || Task Login
                    Toast.makeText(getApplicationContext(),"Successfully Login",Toast.LENGTH_LONG).show();

                        // If the users is professor but if it is student go to userStudent.class
                        finish();
                        startActivity(new Intent(getApplicationContext(),userProf.class));

FireBase Realtime Database

This is what you are looking for

FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser(); 
DatabaseReference reference;
firebaseAuth.signInWithEmailAndPassword(email,pass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    progressDialog.dismiss();
                    if(task.isSuccessful()){

                        //The task is successful || Task Login
                       Toast.makeText(getApplicationContext(),"Successfully Login",Toast.LENGTH_LONG).show(); 
                       reference = FirebaseDatabase.getInstance().
                       getReference("dbname").child(user.getUid());


                       reference.addListenerForSingleValueEvent(new ValueEventListener() {
                       @Override
                       public void onDataChange(DataSnapshot dataSnapshot) {
                       for(DataSnapshot datas: dataSnapshot.getChildren()){
                       String usertype=datas.child("user").getValue().toString();

                       // If the users is professor but if it is 
                            // student go to userStudent.class
                       if(usertype.equals("Student")){

                         startActivity(new      
                         Intent(getApplicationContext(),studentProfile.class));
                         finish();

                       }else if (usertype.equals("Professor")) {
                         startActivity(new      
                         Intent(getApplicationContext(),professorProfile.class));
                         finish();
                       }

                      }
                    }

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






    }

Try this code

firebaseAuth.signInWithEmailAndPassword(email,pass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            progressDialog.dismiss();

            if(task.isSuccessful()){

                //The task is successful || Task Login
                Toast.makeText(getApplicationContext(),"Successfully Login",Toast.LENGTH_LONG).show();

                 //user UID
                String userId = task.getResult().getUser().getUid();
                DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child(userId);

                ref.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                        String user = dataSnapshot.child("user").getValue(String.class);

                        if (user.equals("Student")) {

                         startActivity(new Intent(getApplicationContext(),userStudent.class));

                        } else if(user.equals("Professor")) {
                          startActivity(new Intent(getApplicationContext(),userProf.class));
                        }
                        finish();      

                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
               });

}

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM