In my database, I'm keeping track of the distance travelled by users and set an initial value of 0 for everyone. In my code, I am updating this value by getting the old value and adding the new distance to it, then saving that value.
At first, when I retrieve the 0 from the database, it is returned as a Long, which is fine:
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Long previousDistance = (Long) dataSnapshot.child("distance").getValue();
userReference.child("distance").setValue(previousDistance.doubleValue()+distanceTravelled);
}
The problem is that now, the value stored in the database is a Double, so next time the above code is run it will fail with a ClassCastExcepiton.
I could probably solve the issue with an if statement, but the easiest solution would be to store the initial 0 as a Double instead. However, I can't make that work. I've tried both of these:
myDatabase.child("users").child(lowerCaseUserName).child("distance").setValue(Double.valueOf(0.0));
myDatabase.child("users").child(lowerCaseUserName).child("distance").setValue(0.0);
They still get stored as "0" and returned as a Long. Is it possible to store zero as a Double in the database?