David Bowen
David Bowen

Reputation: 49

Firebase:Failed to convert value of type java.lang.Boolean to String

Hi am trying to check in my database for a boolean of false, however I am getting an error of Failed to convert value of type java.lang.Boolean to String.

Structure of DB

   Query q = databaseReference.child("Chatmessages").child(currentChatUser).child(userId);
      ValueEventListener eventListener = new ValueEventListener() {
          @Override
          public void onDataChange(DataSnapshot dataSnapshot) {
              String true3 = "true";
              for(DataSnapshot ds : dataSnapshot.getChildren()) {


                  if(ds.child("read").getValue(String.class).equals(false)){
                      ds.child("read").getRef().setValue(true);
                  }
              }
          }
          @Override
          public void onCancelled(DatabaseError databaseError) {}
      };
      q.addListenerForSingleValueEvent(eventListener);

Upvotes: 1

Views: 2651

Answers (2)

cutiko
cutiko

Reputation: 10527

Your comparison should work but you have "true" value as String in your Database, Firebase supports using boolean as the value of nodes, so that should be your first fix. For doing it so, simply remove both " surrounding the true value in the database, you can edit it in the web console. Make sure that when you create data you created as boolean primitive datatype instead of String.

And your if condition should be:

if (!ds.child("read").getValue(Boolean.class)){
    //work here
}

In the above condition a negation ! is being used to say: if the opposite of true, then...

Upvotes: 0

Frank van Puffelen
Frank van Puffelen

Reputation: 599641

Your comparison is wrong:

ds.child("read").getValue(String.class).equals(false)

There's no way that the String value that you read will ever be true to the Boolean value false.

Either do a string to string comparison:

ds.child("read").getValue(String.class).equals("false")

Or convert the string to a boolean first and then compare:

Boolean.valueOf(ds.child("read").getValue(String.class)).equals(false)

Upvotes: 1

Related Questions