skip to Main Content

I am currently trying to get my android app to give me a variable based on which button in a radio group I have clicked. As of right now it is only returning either null or the default value I have set no matter how I try to do the Kotlin side of the coding. The group has five buttons, and each one represents a different number. I am trying to pass that number to the next activity as a variable, but since the variables are declared within if statements, the variable is not recognized when I am trying to send it to the next activity. What I have right now is:

                val companyName = findViewById<EditText>(R.id.companyNameText).text.toString()
                var hbv = "3"
                findViewById<RadioGroup>(R.id.hb).setOnCheckedChangeListener { _, checkedId ->
                    if(checkedId == R.id.hb1) {
                        var hbv = "1"
                    }
                    if(checkedId == R.id.hb2) {
                        var hbv = "2"
                    }
                    if(checkedId == R.id.hb3) {
                        var hbv = "3"
                    }
                    if(checkedId == R.id.hb4) {
                        var hbv = "4"
                    }
                    if(checkedId == R.id.hb5) {
                        var hbv = "5"
                    }
                }
                val intent = Intent(this, SecondActivity::class.java)
                intent.putExtra("companyName", companyName)
                intent.putExtra("hbv", hbv)

The companyName variable is taken from an EditText field and passes without any issues. I also have 3 of these radio groups, each containing 5 buttons, but they will all work the same once I get it working. Is there an easy way to set hbv within the if statement or a way to set it equal to the text of the radio button (1, 2, 3, 4, and 5 are the text attributes).

2

Answers


  1. You are introducing a new local hbv variable within each if block instead of assigning a new value to the existing one. Change your code as follows:

    var hbv = "3"
    findViewById<RadioGroup>(R.id.hb).setOnCheckedChangeListener { _, checkedId ->
        if(checkedId == R.id.hb1) {
            hbv = "1"
        }
        if(checkedId == R.id.hb2) {
            hbv = "2"
        }
        if(checkedId == R.id.hb3) {
            hbv = "3"
        }
        if(checkedId == R.id.hb4) {
            hbv = "4"
        }
        if(checkedId == R.id.hb5) {
            hbv = "5"
        }
    }
    val intent = Intent(this, SecondActivity::class.java)
    intent.putExtra("companyName", companyName)
    intent.putExtra("hbv", hbv)
    

    If you create a var inside an if block, the variable only is scoped to that if block, so when you finally pass the number to the next activity, you are only passing the value of the outer hbv variable, which always is going to be 3.

    Login or Signup to reply.
  2. It seems like you are redeclaring the hbv variable inside each if block, which creates a new variable with a limited scope within that block. To resolve this, you should declare hbv outside of the setOnCheckedChangeListener scope and assign values to it inside the blocks without redeclaring it. Here’s an updated version of your code:

     val companyName = findViewById<EditText>(R.id.companyNameText).text.toString()
    var hbv = "3" // Default value
    
    findViewById<RadioGroup>(R.id.hb).setOnCheckedChangeListener { _, checkedId ->
        hbv = when (checkedId) {
            R.id.hb1 -> "1"
            R.id.hb2 -> "2"
            R.id.hb3 -> "3"
            R.id.hb4 -> "4"
            R.id.hb5 -> "5"
            else -> "3" // Default value if none of the radio buttons are selected
        }
    }
    
    val intent = Intent(this, SecondActivity::class.java)
    intent.putExtra("companyName", companyName)
    intent.putExtra("hbv", hbv)
    

    hbv is declared outside the setOnCheckedChangeListener in this version and updated based on the selected radio button inside the block. The when expression simplifies the logic of mapping each radio button to its corresponding value.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search