输出未在文本视图和图像中显示 我输入toast来查看是否正在showdata中获取数据,但显示为null。
FirebaseUser user = mAuth.getCurrentUser();
userid = user.getUid();
myRef= FirebaseDatabase.getInstance().getReference().child("User").child(userid);
Toast.makeText(Account.this, myRef.toString(), Toast.LENGTH_SHORT).show();
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Showdata(dataSnapshot);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(Account.this, "Error", Toast.LENGTH_SHORT).show();
}
});
private void Showdata(DataSnapshot dataSnapshot) {
for(DataSnapshot ds:dataSnapshot.getChildren()) {
String name = ds.child("name").getValue(String.class);
String address= ds.child("address").getValue(String.class);
String contact = ds.child("contact").getValue(String.class);
String profilepic = ds.child("profilepic").getValue(String.class);
Toast.makeText(Account.this, name, Toast.LENGTH_SHORT).show();
//display
P_name.setText(name);
P_number.setText(contact);
P_address.setText(address);
url = profilepic;
}
Glide.with(Account.this /* context */)
.load(url)
.into(P_pic);
progressdialog.dismiss();
}
Firebase数据库
提前谢谢你,天哪!
答案 0 :(得分:1)
尝试一下。
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
// You can access the values here, no need to loop. Store it on your custom object, in this case User
User user = dataSnapshot.getValue(User.class);
Toast.makeText(Account.this, user.getName(), Toast.LENGTH_SHORT).show();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(Account.this, "Error", Toast.LENGTH_SHORT).show();
}
});
答案 1 :(得分:0)
从showData方法中删除for循环。由于您已经在用户ID节点中。只需从datasnapshot获取数据即可。
private void Showdata(DataSnapshot dataSnapshot) {
String name = dataSnapshot.child("name").getValue(String.class);
String address= dataSnapshot.child("address").getValue(String.class);
String contact = dataSnapshot.child("contact").getValue(String.class);
String profilepic = dataSnapshot.child("profilepic").getValue(String.class);
Toast.makeText(Account.this, name, Toast.LENGTH_SHORT).show();
//display
P_name.setText(name);
P_number.setText(contact);
P_address.setText(address);
url = profilepic;
}