0

I'm trying to make a condition on firebase, taking value, but I'm not sure how to do it.

I need to do this, if the value is horizontal do this, if you do not do that.

My firebase:

leitura: "horizontal"

String emailUsuario = autenticacao.getCurrentUser().getEmail();
    String idUsuario = CustomBase64.codificarBase64( emailUsuario );
    favoritosRef = firebaseRef.child("usuarios").child(idUsuario);
    valueEventListenerLeitor = favoritosRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.child("leitura").getValue() == "horizontal") {
                Log.e("TAG", "=======" + dataSnapshot.child("leitura").getValue());
            } else {
                Log.e("TAG", "=======" + "Vertical");
            }
        }



        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
OliverDamon
  • 147
  • 9

2 Answers2

1
valueEventListenerLeitor = favoritosRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.child("leitura").getValue().equals("horizontal")) {
            Log.e("TAG", "=======" + dataSnapshot.child("leitura").getValue());
        } else {
            Log.e("TAG", "=======" + "Vertical");
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});
Raj
  • 2,997
  • 2
  • 12
  • 30
  • 1
    You should explain what the problem was and what you did to fix it and why that works instead of just dumping code here. – takendarkk Jul 30 '18 at 15:42
1

You're trying to compare String with ==, which is wrong. Please try to use equals() as so:

String emailUsuario = autenticacao.getCurrentUser().getEmail();
    String idUsuario = CustomBase64.codificarBase64( emailUsuario );
    favoritosRef = firebaseRef.child("usuarios").child(idUsuario);
    valueEventListenerLeitor = favoritosRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.child("leitura").equals("horizontal")) {
                Log.e("TAG", "=======" + dataSnapshot.child("leitura").getValue());
            } else {
                Log.e("TAG", "=======" + "Vertical");
            }
        }


        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
Gil Becker
  • 283
  • 1
  • 9