我的firebase结构如下所示。
我有两种类型的用户。获取快照时,如何根据Firebase中存储的类型实例化对象。我的代码在下面
if(task.isSuccessful()){
DatabaseReference databaseReference;
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
if(firebaseUser != null){
databaseReference = firebaseDatabase.getReference("User").child(firebaseUser.getUid());
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
//HOW CAN I FIX THIS TO BE THE CORRECT USER TYPE
User user = dataSnapshot.getValue(User.class);
Intent intent = null;
if(user != null){
//IF USER IS TYPE 1, CREATE USER OF TYPE 1 AND ADD STUFF TO MY INTENT
//IF USER IS TYPE 2, CREATE USER OF TYPE 2 AND ADD STUFF TO MY INTENT
finish();
startActivity (intent);
}
}
}
}
答案 0 :(得分:0)
为此,您只需从数据库中检查节点userTypeId
的值,然后执行所需的操作即可。
我的意思在代码中看起来像这样:
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(user != null){
if(dataSnapshot.child("userTypeId").getValue(String.class).equals(0))
// user type 1
else
// user type 2
finish();
startActivity (intent);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.d(TAG, "onCancelled", databaseError.toException());
}
});