2017-02-16 16 views
1

另一個孩子,我有一個登錄和登錄裏面我需要檢查,如果用戶爲了知道至極屬於「Administrador」或「Chofer」(firbase數據庫樹的孩子的)屏幕I'm要打開,我有以下代碼:檢查是否屬於一個按鍵或在火力

mAuth.signInWithEmailAndPassword(usuario, pass) 
    .addOnCompleteListener(Login.this, new OnCompleteListener<AuthResult>() { 
     @Override 
     public void onComplete(@NonNull Task<AuthResult> task) { 

      if (!task.isSuccessful()) { 

       Toast.makeText(Login.this, "Los Datos administrados no son correctos", 
         Toast.LENGTH_SHORT).show(); 
       mProgress.dismiss(); 
      } else{ 
       Toast.makeText(Login.this, "Bienvenido... ", 
         Toast.LENGTH_SHORT).show(); 
       //loadAdmin(task.getResult().getUser()); 
       mProgress.dismiss(); 
       if(AdminFirebase.esAdmin(task.getResult().getUser())){ 
        AdminFirebase.loadAdmin(task.getResult().getUser()); 
        Intent intento=new Intent(Login.this, Principal.class); 
        intento.putExtra("Usuario", "administrador"); 
        startActivity(intento); 
       } 
       else{ 
        ChoferFirebase.loadChofer(task.getResult().getUser()); 
        Intent intento=new Intent(Login.this, Principal.class); 
        intento.putExtra("Usuario", "chofer"); 
        startActivity(intento); 
       } 

      } 
     } 
    }); 

,我有與它下面的一個問題的方法:

public boolean esAdmin(FirebaseUser user){ 

DatabaseReference adminRef= ref.child("Administradores"); 
     if(adminRef.child(user.getUid()).getKey() == null) { 
      return false; 
     } else { 
      return true; 
     } 
    } 

這裏其JSON結構從火力點:

"Administradores" : { 
    "2XPtAqaBDHXm8R5rLWjJ2pIzADi1" : { 
     "chapas" : "bbb222,idTaxi", 
     "email" : "gastondelacruz[email protected]", 
     "nombre" : "gaston", 
     "telefono" : "122323232" 
    } 
    }, 
    "Choferes" : { 
    "6YWaOqBDDlOmrcpgm84dm5WipmH3" : { 
     "imagen" : "https://firebasestorage.googleapis.com/v0/b/taxiexpress-192df.appspot.com/o/fotos%2F86?alt=media&token=4d9bbf4b-f884-459a-820a-ee1027668f1c", 
     "nombre" : "ccccc", 
     "taxi" : "aaa111" 
    }, 
    "6lvl464H3IYfFJOBoE9tzl4IzVu1" : { 
     "imagen" : "https://firebasestorage.googleapis.com/v0/b/taxiexpress-192df.appspot.com/o/fotos%2F35?alt=media&token=1bdb1d06-e4a5-41e7-a5ae-9f8a77d1f515", 
     "nombre" : "facuputito" 
    }, 
    "7gOzX9azHSQLP9Enji0wZMVKYzz1" : { 
     "imagen" : "https://firebasestorage.googleapis.com/v0/b/taxiexpress-192df.appspot.com/o/fotos%2F32?alt=media&token=7d6013a6-4063-4b1f-9171-209ede2562d6", 
     "nombre" : "test2" 
    }, 
    "8iE2DPGY0OWh4sbRTyCtWaDyUct1" : { 
     "imagen" : "https://firebasestorage.googleapis.com/v0/b/taxiexpress-192df.appspot.com/o/fotos%2F86?alt=media&token=f28567aa-5514-48f4-957d-b413f14867ee", 
     "nombre" : "bbbbb" 
    }, 
    "BT1C40DMHRac7LRHuaQQmfeFe7H3" : { 
     "imagen" : "https://firebasestorage.googleapis.com/v0/b/taxiexpress-192df.appspot.com/o/fotos%2F35?alt=media&token=5b160731-221a-44e2-9362-e5b4edf9dde9", 
     "nombre" : "name" 
    }, 
    "LpD2brAzIPTwpP5BFLd0ie8lAX92" : { 
     "imagen" : "https://firebasestorage.googleapis.com/v0/b/taxiexpress-192df.appspot.com/o/fotos%2F86?alt=media&token=1681459e-f25b-4ea3-9599-756cf6617a46", 
     "nombre" : "kakaka" 
    }, 
    "MTYu8GWex8Ox3hXEPhjTwFlmx463" : { 
     "imagen" : "https://firebasestorage.googleapis.com/v0/b/taxiexpress-192df.appspot.com/o/fotos%2F86?alt=media&token=2a0075c1-8e4a-4a21-9ea4-b3f03123989c", 
     "nombre" : "aaaaa" 
    }, 
    "Ndtq2knyyMMs9AlU1ACTiSm4twE3" : { 
     "imagen" : "https://firebasestorage.googleapis.com/v0/b/taxiexpress-192df.appspot.com/o/fotos%2F35?alt=media&token=c67af765-d9b3-4262-b047-01f1ec353dc6", 
     "nombre" : "facutragasable" 
    }, 
    "jdzkv8PrHmYUuRU1lCgoGZgTeQ12" : { 
     "imagen" : "https://firebasestorage.googleapis.com/v0/b/taxiexpress-192df.appspot.com/o/fotos%2F35?alt=media&token=c67af765-d9b3-4262-b047-01f1ec353dc6", 
     "nombre" : "facu" 
    } 
    } 
+0

你能提供你的數據庫結構(JSON如果可能的話),並指出(在數據庫中),你在哪裏放置值,指示用戶是管理員或chofer? – koceeng

+0

喜隊友,對不起,我剛纔看到這個延遲,我已經貼了JSON代碼,並添加了「adminRef」變量的值。 –

回答

0

試試這個:代碼示例中

// make this variables can be accessed for whole class 
int isAdmin = -1; 
int isChofer = -1; 

... 

// then you need to check if user uid is in "Administradores" or "Choferes" 

// check inside "Administradores" 
DatabaseReference adminRef= ref.child("Administradores"); 
adminRef.child(user.getUid()).addValueEventListener(new ValueEventListener() { 
    @Override 
    public void onDataChange(DataSnapshot dataSnapshot) { 
     isAdmin = dataSnapshot.exists() ? 1 : 0; 
     validateUser(); 
    } 
    ... 
}); 

// check inside "Choferes" 
DatabaseReference choferRef= ref.child("Choferes"); 
choferRef.child(user.getUid()).addValueEventListener(new ValueEventListener() { 
    @Override 
    public void onDataChange(DataSnapshot dataSnapshot) { 
     isChofer = dataSnapshot.exists() ? 1 : 0; 
     validateUser(); 
    } 
    ... 
}); 

... 

// then validate each time one of those two event listener get value, 
// you validate is it exist in "Administradores" or in "Choferes" 

private void validateUser() { 
    if (isAdmin > 0 && isAdmin == 1) { 
     // user is admin 
    } else if (isChofer > 0 && isChofer == 1) { 
     // user is chofer 
    } 

    // if both method above is false then it means 
    // one or all of value event listener have not finished 
} 

我添加了註釋,希望有助於理解它是如何工作的。它實際上是一個簡單的方法,如果你讀過Firebase Guide on Read and Write Data on Android

+0

喜伴侶,非常感謝您的回答,事情的,我需要的東西等待,直到該火力點線程完成的過程中,如果你在我的代碼看,我在那裏我要求的結果,所以我可以打開管理的條件屏幕或chofer屏幕,給你添麻煩了:( –

+0

如果你需要等待的是「東西」是'onCompleteListener'則必須經過'Toast.makeText把'adminRef'和'choferRef'值聽衆直接(Login.this 「比恩韋尼......」,Toast.LENGTH_SHORT).show();'然後把從'mProgress.dismiss(代碼);'直到裏面'的ValidateUser()'(與更換結束了'如果else'條件像我的代碼示例) – koceeng

+0

非常感謝您的幫助伴侶。 –