0

I have a problem with changing value of parameter in android studio, kotlin. I want to change boolean value of a particular item in enum class.

Here's the MainActivity.kt:

//        Variables
        val intentMainActivityToShow = Intent(this@MainActivity, ShowData::class.java)


//        Select and apply choice
        radioButtonGroup.setOnCheckedChangeListener { _, checkedId ->

            when (checkedId) {

                R.id.radioButton1 -> {

                    intentMainActivityToShow.putExtra(Variables.Companion.AvailableChoices.FIRST.chosen, true)

                }

                R.id.radioButton2 -> {

                    intentMainActivityToShow.putExtra(Variables.Companion.AvailableChoices.SECOND.chosen, true)

                }

                R.id.radioButton3 -> {

                    intentMainActivityToShow.putExtra(Variables.Companion.AvailableChoices.THIRD.chosen, true)

                }

            }
        }

And here's the Variables.kt:

package com.example.studyingsendingdata

class Variables {

    companion object {

        enum class AvailableChoices (var chosen: Boolean) {

            FIRST(false),
            SECOND(false),
            THIRD(false)

        }
    }
}

Here's the ShowData.kt which displays updated values of parameter:

package com.example.studyingsendingdata

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

class ShowData : AppCompatActivity() {

    lateinit var button: Button
    lateinit var displayResult: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_show_data)

        button = findViewById(R.id.button)
        displayResult = findViewById(R.id.displayResult)

        val getData = intent.extras // extras is the data which is applied via putExtra

        button.setOnClickListener {

            displayResult.text = getData?.getString(Variables.Companion.AvailableChoices.FIRST.chosen)
            displayResult.text = getData?.getString(Variables.Companion.AvailableChoices.SECOND.chosen)
            displayResult.text = getData?.getString(Variables.Companion.AvailableChoices.THIRD.chosen)

        }
    }
}

Every time I click any button the value of paremeter "chosen" remains the same. I suppose I change the value of the parameter only in the MainActivity.kt, not in the Variables.kt. How to fix the code to change the value of a particular item "globally"?

Marcel
  • 15
  • 3

0 Answers0