如何使用以下代码从 Firebase 获取数据?
登录成功后出现空指针异常?
DatabaseReference mref = FirebaseDatabase.getInstance().getReference().child("/Users/Students")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid());
mref.addValueEventListener(new ValueEventListener() {
@SuppressLint("SetTextI18n")
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()){
email = ds.child("email").getValue().toString();
full_name = ds.child("full_name").getValue().toString();
roll_number = ds.child("roll_number").getValue().toString();
admission_id = ds.child("admission_id").getValue().toString();
phone_no = ds.child("phone_number").getValue().toString();
branch = ds.child("branch").getValue().toString();
year = ds.child("year").getValue().toString();
semester = ds.child("semester").getValue().toString();
section = ds.child("section").getValue().toString();
course = ds.child("course").getValue().toString();
batch = ds.child("batch").getValue().toString();
dob = ds.child("dob").getValue().toString();
address = ds.child("address").getValue().toString();
}
}
我的 Firebase 数据库是:
最佳答案
删除以下内容:
for (DataSnapshot ds : dataSnapshot.getChildren()){
由于您已经可以访问 uid,因此您不需要在直接子级中循环来获取值..
所以就这样做:
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
email = dataSnapshot.child("email").getValue().toString();
full_name = dataSnapshot.child("full_name").getValue().toString();
roll_number = dataSnapshot.child("roll_number").getValue().toString();
admission_id = dataSnapshot.child("admission_id").getValue().toString();
phone_no = dataSnapshot.child("phone_number").getValue().toString();
branch = dataSnapshot.child("branch").getValue().toString();
year = dataSnapshot.child("year").getValue().toString();
semester = dataSnapshot.child("semester").getValue().toString();
section = dataSnapshot.child("section").getValue().toString();
course = dataSnapshot.child("course").getValue().toString();
batch = dataSnapshot.child("batch").getValue().toString();
dob = dataSnapshot.child("dob").getValue().toString();
address = dataSnapshot.child("address").getValue().toString();
关于java - 如何从 android 中的 firebase 数据库中的 uid 获取/检索多个数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51613536/