From 94af938971575823ef64090e28763c47acd662a0 Mon Sep 17 00:00:00 2001 From: ps428 <57292159+ps428@users.noreply.github.com> Date: Fri, 18 Dec 2020 02:40:21 +0530 Subject: [PATCH] Added more efficient code for DiceRollerFinal In this code, instead of using lateinit, I have modified the rollDice function, so that it has an input parameter of type ImageView, when we call this function in line 45, I have called the diceImage variable that is of type ImageView and "points to" the @id/dice_image. --- .../java/com/example/android/diceroller/MainActivity.kt | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/DiceRollerFinal/app/src/main/java/com/example/android/diceroller/MainActivity.kt b/DiceRollerFinal/app/src/main/java/com/example/android/diceroller/MainActivity.kt index 4ba9b4093..7f5a9a105 100644 --- a/DiceRollerFinal/app/src/main/java/com/example/android/diceroller/MainActivity.kt +++ b/DiceRollerFinal/app/src/main/java/com/example/android/diceroller/MainActivity.kt @@ -31,7 +31,6 @@ import java.util.* */ class MainActivity : AppCompatActivity() { - lateinit var diceImage : ImageView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -40,14 +39,16 @@ class MainActivity : AppCompatActivity() { // Get the Button view from the layout and assign a click // listener to it. val rollButton: Button = findViewById(R.id.roll_button) - rollButton.setOnClickListener { rollDice() } - diceImage = findViewById(R.id.dice_image) + + val diceImage: ImageView = findViewById(R.id.dice_image) + + rollButton.setOnClickListener { rollDice(diceImage) } } /** * Click listener for the Roll button. */ - private fun rollDice() { + private fun rollDice(diceImage: ImageView) { // Toast.makeText(this, "button clicked", // Toast.LENGTH_SHORT).show() val randomInt = (1..6).random()