问题描述
如何在Android Studio中从POJO获取值并将其值设置为TextView
MainActivity.java onCreate方法:
How can I get value from POJO and set its value to TextView
in android studioMainActivity.java onCreate method:
FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
assert user != null;
String UserID=user.getUid();
UserInfoPOJO userinfopojo = new UserInfoPOJO(UserID);
UserInfoPOJO.java
public class UserInfoPOJO {
private String UserID;
public UserInfoPOJO(String userID) {
UserID = userID;
}
public String getUserID() {
return UserID;
}
public void setUserID(String userID) {
UserID = userID;
}
}
Main2Activity.java 的
onCreate 方法包含:
请在这里教我如何设置textview的值
onCreate method of Main2Activity.java contains:
please teach me how to set value to textview here
userid1 = findViewById(R.id.op_uid);
userinfopojo = (UserID);
userid1.setText();
我试图制作的快照
这不包含在上面的问题中.
我想根据Uid存储数据
Snapshot of what I was trying to make
this is not included in the question above.
I want to store data according to Uid
推荐答案
如果bVVz ... i322
是已登录用户的ID,请获取email
,mobile
和name
的值.将UserInfoPOJO
类更改为:
If the bVVz ... i322
is the id of the logged-in user, to get the values of email
, mobile
and name
, please change the UserInfoPOJO
class to:
class UserInfoPOJO {
public String email, mobile, name;
public UserInfoPOJO() {}
}
并且要将对象从数据库映射到类型为UserInfoPOJO
的对象,请使用以下代码行:
And to map the object from the database to an object of type UserInfoPOJO
, please use the following lines of code:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
UserInfoPOJO userInfoPOJO = dataSnapshot.getValue(UserInfoPOJO.class);
String email = userInfoPOJ.email;
String mobile = userInfoPOJ.mobile;
String name = userInfoPOJ.name;
Log.d("TAG", email + "/" + mobile + "/" + name);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage()); //Don't ignore errors!
}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);
您的logcat中的输出将是:
The output in your logcat will be:
something@example.com/888888888/person name
这篇关于如何从POJO获取值并将值设置为TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!