简体   繁体   English

从Firebase实时数据库中检索特定数据

[英]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数据库中检索特定数据。我有2个用户Professor和Student,并且我想使用该数据进行验证。

FireBase Realtime Database FireBase实时数据库

在此处输入图片说明

 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 FireBase实时数据库

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 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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