0
I want to take the user data registered in Firebase and put them in a Textview, but I’ve tried several ways and none worked.
Code of Profile Fragment:
public class PerfilFragment extends Fragment {
private FragmentPerfilBinding binding;
private FirebaseAuth mAuth;
private DatabaseReference mDatabase;
private String usuarioID;
private UserModel userModel;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
binding = FragmentPerfilBinding.inflate(inflater, container, false);
View root = binding.getRoot();
final TextView textViewNome = binding.textViewNome;
final TextView textViewSobrenome = binding.textViewSobrenome;
final TextView textViewEmail = binding.textViewEmail;
final Button buttonLogout = binding.buttonLogout;
mAuth = FirebaseAuth.getInstance();
String email = FirebaseAuth.getInstance().getCurrentUser().getEmail();
usuarioID = FirebaseAuth.getInstance().getCurrentUser().getUid();
mDatabase = FirebaseDatabase.getInstance().getReference("usuarios");
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) {
textViewEmail.setText(snapshot.getChildren(""));
}
@Override
public void onCancelled(@NonNull @NotNull DatabaseError error) {
}
});
//funções button Logout
buttonLogout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mAuth.signOut();
Intent intent = new Intent(getActivity(), activityLogin.class );
startActivity(intent);
}
});
return root;
}
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
Here is the class I used to create the user for the registration (if it is needed).
public class UserModel {
private String id;
private String nome;
private String sobrenome;
private String email;
public UserModel() {
}
public UserModel(String id, String nome, String sobrenome, String email) {
this.id = id;
this.nome = nome;
this.sobrenome = sobrenome;
this.email = email;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getSobrenome() {
return sobrenome;
}
public void setSobrenome(String sobrenome) {
this.sobrenome = sobrenome;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public void salvar(){
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
reference.child("usuarios").child(getId()).setValue(this);
}
}
The database is with registered users, and I wanted to take this data with name, surname and email and pass to a Textview.