簡體   English   中英

從Firebase實時數據庫中檢索特定數據

[英]Retrieving Specific Data from Firebase Realtime Database

請幫助我從Firebase Realtime數據庫中檢索特定數據。我有2個用戶Professor和Student,並且我想使用該數據進行驗證。

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實時數據庫

這就是你要找的

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) {
                    }
                  });






    }

試試這個代碼

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) {

                    }
               });

}

希望能幫助到你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM