How do I get data from Firebase and insert it into a TextView?

5

I need to define the client data in a TextView. I tried setting the email, but it did not work, because the return is null.

public class PerfilActivity extends AppCompatActivity {
    private TextView tv_email;
    private FirebaseAuth auth;
    private Usuario usuario = new Usuario();
    private DatabaseReference firebaseDatabase;
    private ValueEventListener valueEventListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_perfil);

        tv_email = findViewById(R.id.tv_email_perfil);
        auth = FirebaseAuth.getInstance();

        firebaseDatabase = ConfiguracaoFirebase.getFirebaseDatabase()
                .child("usuarios/clientes"+ usuario.getUid()+ "/email");

        valueEventListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                tv_email.setText(String.valueOf(dataSnapshot.getValue()));
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        };
    }
}

    
asked by anonymous 16.12.2017 / 21:24

4 answers

3

The problem of dataSnapshot being null is that your DatabaseReference is not correct. That is, there is no data to be collected in this path passed to DatabaseReference.

For each level of data you need to call .child() again.

Change your DatabaseReference to this structure:

firebaseDatabase = ConfiguracaoFirebase.getFirebaseDatabase()
.child("usuarios").child("clientes").child(usuario.getUid())

In addition, it is recommended that you create a template class for your data in Firebase.

For example:

@IgnoreExtraProperties
public class Usuario {

private String id;
private String email;
private String senha;
private String nome;
private String cpfCnpj
private String telefone;
private String endereco;
private int credito;

    public Usuario() {
        // Construtor obrigatório para as chamadas do DataSnapshot.getValue(Usuario.class)
    }

    public Usuario(String id, String email, String senha, String nome, String cpfCnpj, String telefone, String endereco, int credito) {
        this.id = id;
        this.email = email;
        this.senha = senha;
        this.nome = nome;
        this.cpfCnpj = cpfCnpj;
        this.telefone = telefone;
        this.endereco = endereco, 
        this.credito = credito;
    }

} 

In this way, you only have to call DataSnapshot.getValue(Usuario.class) within ValueEventListener() and you will have the User object with data already loaded from Firebase.

I recommend reading Realtime Database documentation for search for references.

    
21.12.2017 / 00:15
2

If you just want to get the value of the email, you should do:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference("usuarios");
DatabaseReference clientesref = ref.child("clientes");
DatabaseReference email = clientesref.child("email");

However, this way of programming is not the best. You should have a Java class with the attributes email, name, have, phone, etc and you should read them automatically at once. It would be something like this:

valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
        if(dataSnapshot.hasChildren()) {
                <classe criada por ti> a = dataSnapshot.getValue(<classe criada por ti>.class);

                filldata(a.getEmail());
    }
}

public void filldata(String email){
    TextView a = (TextView) findViewforId(R.id.email);

    a.setText(email);

}

Where then in% w / o% you only need to make% w /% to your class.

Take a look at this example .

    
17.12.2017 / 13:25
0

I believe that the fault is here private Usuario usuario = new Usuario(); , you instantiate the object with all its null attributes and then try to capture an attribute of it, however it will be null, so your request is calling "usuarios/null/email" . To correct this you will need to feed the user's UID.

I hope this solution will help you.

    
22.12.2017 / 17:14
0

You just created the instance of the User object, but did not initialize its attributes, so uid returns null. Initialize user uid with FirebaseAuth uid:

public class PerfilActivity extends AppCompatActivity {
    private TextView tv_email;
    private FirebaseAuth auth;
    private Usuario usuario = new Usuario();
    private DatabaseReference firebaseDatabase;
    private ValueEventListener valueEventListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_perfil);

        tv_email = findViewById(R.id.tv_email_perfil);
        auth = FirebaseAuth.getInstance();

        usuario.setUid(auth.getCurrentUser().getUid());

        firebaseDatabase = ConfiguracaoFirebase.getFirebaseDatabase()
                .child("usuarios/clientes/"+ usuario.getUid()+ "/email");

        valueEventListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                tv_email.setText(dataSnapshot.getValue(String.class));
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        };
    }
}
    
25.12.2017 / 15:44