Home > Software engineering >  Do handlers postDelays not work with for loops I've tried several ways, but cant get it to work
Do handlers postDelays not work with for loops I've tried several ways, but cant get it to work

Time:09-17

pls pardon the bad way I program at the moment, I'm very new to programming.

Okay, let's say I making a simple dnd dice roller(cause I am). I made it so it rolls a bunch of random numbers based on how many dice they want rolled and the type of dice. it then sends it to a text view, one at a time believe. I wanted to add a short delay, so I asked stackOverFlow, and got a handler working I believe; however, for the same reason I needed delay for in the first place. It only shows the last number rolled on the display with a now short delay between button presses. I would like show the user each number rolled one at a time with a short delay between each.

Example of the code without handler:

else if (numTimesRolled.progress <= 4) {
                for (i in 0 until numTimesRolled.progress   1){
                    randNum = Random.nextInt(1, diceIsComfirm)
                    resultsArray[i] = randNum.toString()
                    randNumDisplay.text = resultsArray[i]
                }
            randNumResultsDisplay.text = "Rolled ${resultsArray.joinToString(" ")}"
        }

Outputs: the last number rolled

Examples of where I tried to use a handler:

else if (numTimesRolled.progress <= 3) {
            for (i in 0 until numTimesRolled.progress   1){
                randNum = Random.nextInt(1, diceIsComfirm)
                resultsArray[i] = randNum.toString()
                    var runResults = Runnable {
                        randNumDisplay.text = resultsArray[i]
                    }
                var delayResults = Handler()
                delayResults.postDelayed(runResults, 1000)
            }
            //for (i in 0 until numTimesRolled.progress   1){

            //}
            randNumResultsDisplay.text = "Rolled ${resultsArray.joinToString(" ")}"
        }

//////////////////////////////////////////////////////////////////////////////////////////////////

else if (numTimesRolled.progress <= 3) {
            for (i in 0 until numTimesRolled.progress   1){
                randNum = Random.nextInt(1, diceIsComfirm)
                resultsArray[i] = randNum.toString()
            }
            for (i in 0 until numTimesRolled.progress   1){
                var runResults = Runnable {
                    randNumDisplay.text = resultsArray[i]
                }
                var delayResults = Handler()
                delayResults.postDelayed(runResults, 1000)
            }
            randNumResultsDisplay.text = "Rolled ${resultsArray.joinToString(" ")}"
        }

Output: both run as normally, but with a delay on the last numbers of the many rolled for each time the button is pressed.

I tried making the whole for loop as the "Runnable," but that was just me being stupid looking for solutions.

CodePudding user response:

PostDelay doesn't wait for the amount of time. It posts a message to a handler specifying when it should do something. To get what you want, each postDelay runnable has to post another post delay.

  • Related