How to get value from POJO and set value to TextView

坚强是说给别人听的谎言 提交于 2020-04-16 02:55:46

问题


How can I get value from POJO and set its value to TextView in android studio MainActivity.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;
    }
}

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();

Snapshot of what I was trying to make

this is not included in the question above.

I want to store data according to Uid


回答1:


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() {}
}

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);

The output in your logcat will be:

something@example.com/888888888/person name


来源:https://stackoverflow.com/questions/60921478/how-to-get-value-from-pojo-and-set-value-to-textview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!