David Bowen David Bowen - 3 years ago 235
Java Question

Firebase check value

in my firebase database I would like to check whether the "read" key is equalled to "false" However, the if statement below does not seem to work.

ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String true3 = "true";
for(DataSnapshot ds : dataSnapshot.getChildren()) {

if( ( ds.child("read").getValue() == ("false"))){
ds.child("read").getRef().setValue(true3);
}

}
}

@Override
public void onCancelled(DatabaseError databaseError) {}
};
q.addListenerForSingleValueEvent(eventListener);

Answer Source

ds.child("read").getValue() will return an Object and obj == "false" will compare reference not their values so use snapshot#getValue(ClassName.class)

  if(ds.child("read").getValue(String.class).equals("false")){
  // and always compare String objects with String#equals

instead of

  if( ( ds.child("read").getValue() == ("false"))){
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download