[英]Retrieve data of a specific user from a Firebase database
I am trying to retrieve data
from Firebase Realtime Database
. 我正在尝试从
Firebase Realtime Database
检索data
。 It's working. 它正在发挥作用。 My question is how to set text from
userDatabase
. 我的问题是如何从
userDatabase
设置文本。 The result will be null
and I think it does not read the database
while using userID
. 结果将为
null
,我认为它在使用userID
不会读取database
。
FirebaseUser user = firebaseAuth.getCurrentUser();
String userid = user.getUid();
txvEmail = (TextView) findViewById(R.id.txvEmail);
txvEmail.setText("Welcome "+user.getEmail());
txvName = (TextView) findViewById(R.id.txvName);
txvAdd =(TextView) findViewById(R.id.txvAdd);
userDatabase = new UserDatabase();
reff = FirebaseDatabase.getInstance().getReference("Member").child(userid);
reff.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
txvName.setText(" "+userDatabase.getFirstName()+" "+userDatabase.getLastName());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
I think you must define your model class to values of snapshots 我认为你必须将你的模型类定义为快照的值
txvEmail = (TextView) findViewById(R.id.txvEmail);
txvEmail.setText("Welcome "+user.getEmail());
txvName = (TextView) findViewById(R.id.txvName);
txvAdd =(TextView) findViewById(R.id.txvAdd);
userDatabase = new UserDatabase();
reff = FirebaseDatabase.getInstance().getReference("Member").child(userid);
reff.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
//new code
for(DataSnapshot snap: dataSnapshot.getChildren()){
//you must define your class model to the value of snapshots
userDatabase user = snap.getValue(userDatabase.Class);
txvName.setText(" "+user.getFirstName()+" "+user.getLastName());
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.