简体   繁体   中英

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

create file in xml folder

<?xml version="1.0" encoding="utf-8"?>
<defaultsMap>
    <entry>
       <key>email</key>
       <value>name</value>
</entry>

public FirebaseRemoteConfig getmFirebaseRemoteConfig() {

    mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();

    FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
             //.setDeveloperModeEnabled(BuildConfig.DEBUG)
            .setDeveloperModeEnabled(false)
            .build();
    mFirebaseRemoteConfig.setConfigSettings(configSettings);

    mFirebaseRemoteConfig.setDefaults(R.xml.remote_config_defaults);


    return mFirebaseRemoteConfig;
}

Assuming that your database structure looks similar to this:

Firebase-root
   |
   --- users
         |
         --- uid
              |
              --- name: "John"
              |
              --- email: "john@email.com"

To print the name and the email, please use the following code using a model class:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("users").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        User user = dataSnapshot.getValue(User.class);
        String name = user.getUserName();
        tName.setText(name);
        String email = user.getUserEmail();
        tEmail.setText(email);
        Log.d("TAG", name + " / " + email);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);

If you want to print them even simpler using the String class, please use the following code:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("users").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String name = dataSnapshot.child("name").getValue(String.class);
        tName.setText(name);
        String email = dataSnapshot.child("email").getValue(String.class);
        tEmail.setText(email);
        Log.d("TAG", name + " / " + email);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);

In both cases, the output will be: John / john@email.com

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM