Receive a single data from a Firebase node

Asked

Viewed 1,040 times

0

I would like to know how to recover from Firebase a single data from Firebase, I used the structure of Eventlistener mounting the object and pulling only one of the data, but when running the error and close app, it may be error in the specified path, but I do not know how to proceed. How to do this recover only 1 of the data from one node ???

Fragment

public class fragmentPerfil extends Fragment {
private DatabaseReference databaseReference;
private FirebaseAuth autenticacao;
private TextView nome;
private String usuarioLogado;

public fragmentPerfil() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_noticias, container, false);

    nome = (TextView) view.findViewById(R.id.txt_nome);

    autenticacao = FirebaseConfig.getAutenticacao();
    autenticacao.getCurrentUser();
    usuarioLogado = autenticacao.getCurrentUser().getUid();
    databaseReference = FirebaseConfig.getFirebase().child("Usuarios").child(usuarioLogado);
    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Usuarios user = dataSnapshot.getValue(Usuarios.class);
            nome.setText(user.getNome());

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });



    return view;
  }

}

inserir a descrição da imagem aqui

1 answer

0


Hello,

FirebaseConfig.getFirebase().child("Usuarios").child(usuarioLogado);

It will return the whole object of "Users", if you want to return only the "name", do so:

FirebaseConfig.getFirebase().child("Usuarios").child(usuarioLogado).child("nome");

Woe to your return in the

 public void onDataChange(DataSnapshot dataSnapshot) {

It will be a string. To catch it would look like this:

String nome = (String) dataSnapshot.getValue();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.