我不明白为什么要立即从 Firebase RealtimeDatabase 读取数据如此困难。我认为除了使用监听器之外别无他法。在我的代码中,我想从对象用户那里读取提交的“tipo”并在屏幕上打印。但它不加载数据
private User user;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mDatabaseAPI = FirebaseRealTimeDatabaseAPI.getInstance();
setFields();
Toast.makeText(this,user.getTipo(),Toast.LENGHT_LONG).show();
}
private void setFields() {
Authentication auth = new Authentication();
user = new User();
user = auth.getCurrentUser();
reference = mDatabaseAPI.getUserReferenceByUid(user.getUid()).child(user.TIPO);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
user.setTipo( dataSnapshot.getValue(Integer.class));
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
白猪掌柜的
相关分类