androidxmlkotlincharcharsequence

How can I use this equation in my TextView?


I'm currently sharing code between activities and on this activity I wrote a function with all the shared data and I am trying to put the outcome of this function in a TextView. But it says it's expecting CharSequence! and that I have type Double. Can someone explain please? Thank you!

Here below is my code, you can see the shared variables that use the getIntExtra function. And the variable recommendedCalories is the equation I'm referring to.

package com.example.optilife

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class WelcomeActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_welcome)
        val welcomeToHomeButton = findViewById<Button>(R.id.SendToHomeButton)
        welcomeToHomeButton.setOnClickListener {
            val intent = Intent(this, HomeActivity::class.java)
            startActivity(intent)
        }
        val userAge = intent.getIntExtra("EXTRA_AGE", 0)
        val userHeight = intent.getIntExtra("EXTRA_HEIGHT", 0)
        val userWeight = intent.getIntExtra("EXTRA_WEIGHT", 0)
        val recommendedCalories = (66 + 13.7userWeight + 5userHeight - 6.8*userAge)
        val caloriesText = findViewById<TextView>(R.id.tv_Test).apply {
            text = recommendedCalories
        }

    }
}

Solution

  • The recommendedCalories is a Double and TextView only accept CharSequence data type. So you only need to convert it to be String by

    findViewById<TextView>(R.id.tv_Test).apply {
        text = recommendedCalories.toString()
    }