1

So this is my Java code that works

if (currentForecastJava.getCurrentObservation().getTempF() >= 60) {
    mCurrentWeatherBox.setBackgroundColor(getResources().getColor(R.color.weather_warm));
    mToolbar.setBackgroundColor(getResources().getColor(R.color.weather_warm));
} else {
    mCurrentWeatherBox.setBackgroundColor(getResources().getColor(R.color.weather_cool));
    mToolbar.setBackgroundColor(getResources().getColor(R.color.weather_cool));
}

What I am trying to do is write this in Kotlin(know AS has the converter but does not change anything)

if (currentObservationKotlin.tempF.compareTo() >=)

    currentWeatherBox.setBackgroundColor(resources.getColor(R.color.weather_warm))
    toolbar.setBackgroundColor(resources.getColor(R.color.weather_warm))

else currentWeatherBox.setBackgroundColor(resources.getColor(R.color.weather_cool))
     toolbar.setBackgroundColor(resources.getColor(R.color.weather_cool))

I know I need a value in the compareTo() and after but I am not really sure what to place as I want to compare TempF to 60 as I want the color to change based on the TempF value from data class. I do not have another object to compare it to.

I can write this in Java and it works with the rest of the Kotlin code but trying to see if Kotlin can make the Java if/else similar and quicker to write.

4
  • Why not just currentObservationKotlin.tempF >= 60? Commented Jun 2, 2017 at 14:54
  • I can use currentObservationKotlin.tempF!! >= 60 and everything compiles and works as expected but not sure why I need to put !!. Still trying to learn the null stuff with Kotlin. Commented Jun 2, 2017 at 15:21
  • When I do as suggested above, I get an error on the >= operator. I states that "None of the following functions can be called with the arguments supplied: public final operator fun compareTo(other: Double): Int defined in kotlin.Double".I can use currentObservationKotlin.tempF!! >= 60 and everything compiles and works as expected but not sure why I need to put !!. Still trying to learn the null stuff with Kotlin. Commented Jun 2, 2017 at 15:34
  • Looks like getCurrentObservation() is declared to return a nullable type. You can't compare a value of a nullable type with anything, because it would produce an NPE if the value was null. Commented Jun 2, 2017 at 15:37

1 Answer 1

1

The Java and Kotlin version would be almost the same. Start with the Java code and drop the semicolons ; and then anything that COULD be nullable needs to be handled with either null checks or you asserting that they will never be null with !!, or using another null operator. You do not show enough code (i.e. the method signature coming into this code, or the declaration of the used variables) to tell you exactly what needs to change.

For handling null values see: In Kotlin, what is the idiomatic way to deal with nullable values

You might end up with warnings around calling a setter method as something.setXyz(value) instead of assigning it as a property something.xyz = value and the IDE will help you fix those or you can live with the warning.

For more about interoperability with JavaBean properties see: Java Interop: Getters and Setters

So with all of that in mind, your final code (with a little more cleaning) might appear something like:

val currentTemp = currentForecastJava.getCurrentObservation()?.getTempF() ?: -1 
// or change -1 to whatever default you want if there is no observation
if (currentTemp >= 60) {
    val warmColor = getResources().getColor(R.color.weather_warm)
    mCurrentWeatherBox.backgroundColor = warmColor
    mToolbar.backgroundColor = warmColor
} else {
    val coolColor = getResources().getColor(R.color.weather_cool)
    mCurrentWeatherBox.backgroundColor = coolColor
    mToolbar.backgroundColor = coolColor
}
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome, thank you so much, will look into both links. I see your using the Elvis operator also. I was trying to do that first, but wasn't able to create the code correctly. Is it always better to create another val/car with Kotlin then just using var currentTemp: Double = currentObservationKotlin?.tempF!! and just setting that to a TextView?
I only created other variables to make the code clearer what steps were doing what. They are optional.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.