I'd like to write a Float so that I can set it's value to null... any ideas?
My only idea now is to write a negative value if the Float is null, then when reading the parcel handle the negative value by writing null. This would be pretty annoying though, lots of if statements (the end goal is to write this JSON object and it needs to contain Floats not floats.
Thanks!
edit:
I'm currently using these methods to do this. All of the values from r.get....() return Floats
private float valueFromFloat(Float f) {
if (f == null) {
return -1f;
} else {
return f.floatValue();
}
}
private void writeRoute(Route r) {
parcel.writeFloat(valueFromFloat(r.getBus()));
parcel.writeFloat(valueFromFloat(r.getHeavy_rail()));
parcel.writeFloat(valueFromFloat(r.getWalk()));
parcel.writeFloat(valueFromFloat(r.getCar23()));
parcel.writeFloat(valueFromFloat(r.getLight_rail()));
parcel.writeFloat(valueFromFloat(r.getCars4()));
parcel.writeFloat(valueFromFloat(r.getCar()));
parcel.writeFloat(valueFromFloat(r.getAlternative()));
parcel.writeFloat(valueFromFloat(r.getMotorcycle()));
}
writeValue works for Float, Double and a few other types
Float x = new Float(3.14);
parcel.writeValue(x);
// later
Float x = parcel.readValue();