Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
243 views
in Technique[技术] by (71.8m points)

java - I cant get the values from Firebase and put them in to TextView in fragment

In my app, I created login and signup page and saved the user name and email to my Firebase database. I also made bottom navigation with fragment. I want to take these values(name, mail) from Firebase and put it into TextView in profile fragment. When I tried this, my app crashed. How can I solve this problem?

public class ProfileFragment extends Fragment {
    private FirebaseDatabase mFirebaseDatabase;
    private FirebaseAuth mAuth;
    private String userID;

    private TextView tName;
    private TextView tEmail;


    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View profileView = inflater.inflate(R.layout.fragment_profile, null);

        tName=profileView.findViewById(R.id.tv_name);
        tEmail=profileView.findViewById(R.id.tv_email);


        mAuth=FirebaseAuth.getInstance();
        mFirebaseDatabase=FirebaseDatabase.getInstance();
        final DatabaseReference myRef=mFirebaseDatabase.getReference("users");
        FirebaseUser user=mAuth.getCurrentUser();
        userID=user.getUid();

        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for(DataSnapshot ds: dataSnapshot.getChildren()){
                    User user=new User();
                    user.setUserName(ds.child(userID).getValue(User.class).getUserName());
                    user.setUserEmail(ds.child(userID).getValue(User.class).getUserEmail());

                    tName.setText(user.getUserName());
                    tEmail.setText(user.getUserEmail());
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {}
        });

        return profileView;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...