He creado un botón al hacer clic que pasa por una lista y saca elementos individuales y los envía a través de Bluetooth al hardware.
Lo que quiero hacer es cuando hago clic en el botón una vez más, debería detener la ejecución del bucle intermedio y no deberían enviarse más valores.
He intentado hacerlo con una variable, que se vuelve verdadero y falso al hacer clic y el ciclo continúa solo cuando la variable es verdadera.
Aquí está:
run.setOnClickListener{
if (!stop){
stop = true
runList()
}
else{
stop = false
}
}
private fun runList() {
for (i in 0 until order.size) {
if(stop) {
//Elements to send come here, using order[i]
}
if (!stop) {
break
}
}
}
Pero en este código, el ciclo continúa hasta el final cuando todos los elementos se completan, incluso si hago clic en el botón nuevamente entre la ejecución. ¿Podría alguien ayudarme?
Solución del problema
run.setOnClickListener{
if (!stop){
stop = true
}else{
stop = false
}
runList();
}
private fun runList() {
if(stop){
for (i in 0 until order.size) {
//Elements to send come here, using order[i]
}
}
No hay comentarios:
Publicar un comentario