Search code examples
androiddatabasefirebasefirebase-realtime-databaseinformation-retrieval

Having issues with retrieving user information from firebase android


Profile Fragment Code

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_profile, container, false);


    Name = (EditText) rootView.findViewById(R.id.Name);
    Email = (EditText) rootView.findViewById(R.id.Email);
    Status = (EditText) rootView.findViewById(R.id.Status);
    Quote = (EditText) rootView.findViewById(R.id.Quote);

    mAuth = FirebaseAuth.getInstance();
    loadUserInformation();
    return rootView;
}

public void loadUserInformation() {

    FirebaseUser user = mAuth.getCurrentUser();

    if (user != null) {
        if (user.getName() != null) {
            Name.setText(user.getName());
        }
        if (user.getEmail() != null) {
            Email.setText(user.getEmail());
        }
        if (user.getStatus() != null) {
            Status.setText(user.getStatus());
        }
        if (user.getQuote() != null) {
            Quote.setText(user.getSQuote());
        }


    }
}

Helper Class

public class SetUpProfileHelper1 {

public String name;
public String email;
public String status;
public String quote;

public SetUpProfileHelper1(String name, String email, String status, String quote) {
    this.email = email;
    this.name = name;
    this.status = status;
    this.quote = quote;
}

public String getName() {
    return name;
}
public String getEmail() {
    return email;
}
public String getStatus() {
    return status;
}
public String getQuote() {
    return quote;
}

}

First the user signs up. then is taken to setupprofile activity where he fill in his details like name, email etc. i have no problem saving this data in Firebase. There is a profile activity where the user should be able to view his details like name, email which he provided after signing up. how do i retrieve the data from Firebase and display it in EditText. Please help


Solution

  • Assuming that you database look something like this:

    Firebase-root
        |
        --- users
              |
              --- uid1
              |     |
              |     --- //details
              |
              --- uid2
                    |
                    --- //details
    

    Please use the following code to get the desired data:

    String uid = mAuth.getCurrentUser().getUid();
    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference uidRef = rootRef.child("ref").child(uid);
    ValueEventListener eventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                SetUpProfileHelper1 obj = ds.getValue(SetUpProfileHelper1.class);
                Log.d("TAG", obj.getName());
            }
        }
    
        @Override
        public void onCancelled(DatabaseError databaseError) {}
    };
    uidRef.addListenerForSingleValueEvent(eventListener);
    

    The output will be, all the names of your users.