Zorgan
Zorgan

Reputation: 9123

Pass parameter from onCreate to onClick

Here is my kotlin/android activity:

import kotlinx.android.synthetic.main.activity_player_details.*

class PlayerDetails : AppCompatActivity(), View.OnClickListener {

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

        val intent = getIntent()
        val numOfPlayers = intent.getIntExtra("number_of_players", 1)    
        next_details.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        for (player in 1..numOfPlayers) {
            // body in here

        }
    }

}

I want to pass the value of numOfPlayers to my onClick method so I can use it - how can I achieve this?

Upvotes: 1

Views: 962

Answers (4)

Bino Benny
Bino Benny

Reputation: 1

Make numOfPlayers global var numOfPlayers =0

In onCreate()

val intent = getIntent()
numOfPlayers = intent.getIntExtra("number_of_players", 1)

In onClick()

for (i in 1..numOfPlayers) {
            // body in here

        }

Upvotes: 0

Shreck Ye
Shreck Ye

Reputation: 1859

onCreate is called when the activity is created, and onClick is called when the next_details button is clicked, which definitely happens after onCreate.

So make numOfPlayers a class member and assign the value to it in onCreate, and also add the lateinit keyword to numOfPlayers so you don't need to make it nullable.

The code should look like this:

import kotlinx.android.synthetic.main.activity_player_details.*

class PlayerDetails : AppCompatActivity(), View.OnClickListener {

    lateinit var numOfPlayers: Integer
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_player_details)

        val intent = getIntent()
        numOfPlayers = intent.getIntExtra("number_of_players", 1)    
        next_details.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        for (player in 1..numOfPlayers) {
            // body in here

        }
    }

}

Upvotes: 0

bhumik
bhumik

Reputation: 231

Either make numOfPlayer as global variable or create separate function to call when btn is clicked and pass numOfPlayer as parameter

1. make numOfPlayer as global variable

class PlayerDetails : AppCompatActivity(), View.OnClickListener {
    var numOfPlayers = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_player_details)

        val intent = getIntent()
        numOfPlayers = intent.getIntExtra("number_of_players", 1)
        next_details.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        for (player in 1..numOfPlayers) {
            // body in here
        }
    }
}

2. create separate function to call when btn is clicked and pass numOfPlayer as parameter

class PlayerDetails : AppCompatActivity(), View.OnClickListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_player_details)
        val intent = getIntent()
        val numOfPlayers = intent.getIntExtra("number_of_players", 1)
        bottomNav.setOnClickListener {
            onNextClick(numOfPlayers)
        }
    }

    private fun onNextClick(numOfPlayers: Int) {
        for (player in 1..numOfPlayers) {
            // body in here
        }
    }
}

Upvotes: 2

Vivek Mishra
Vivek Mishra

Reputation: 5705

Define your variable with global scope like below

var numOfPlayers = 0

Make sure to define it above onCreate() method.

Upvotes: 0

Related Questions