0

So currently I am making a Pokédex for my internship. I've got it working for the biggest part but there's a couple of things I need to get fixed including the question asked above. I have a function called Eeveelutions which has to run three other functions called showVaporeon, showJolteon & showFlareon. They have to run for like 3 seconds each and then just loop around until the person using the pokedex goes to the next or previous pokémon. The thing I need help with is how would I set a timer, (if that's the best way to do it) to run those functions. So, showVaporeon for 3 seconds, then showJolteon for 3 seconds, then Flareon for 3 seconds and repeat. I have searched loads of questions to find my solution but I can't find it yet and most of it is not in kotlin.

So, is there anyone who has got an easy example for me or a better solution (and example) then using a timer.

Searched forum for solutions, messed around with timers, messed around with threads but no solutions yet

fun showVaporeon(){
        evoChart2.visibility = View.VISIBLE
        Glide.with(this).load(imageBaseURL + "134" + ".png").into(evoChart2)
        evolveOption2.text = "Vaporeon"
        evolveOption2.text = ""
        evoChart2.visibility = View.GONE
    }

    fun showJolteon(){
        evoChart2.visibility = View.VISIBLE
        Glide.with(this).load(imageBaseURL + "135" + ".png").into(evoChart2)
        evolveOption2.text = "Jolteon"
        evolveOption2.text = ""
        evoChart2.visibility = View.GONE
    }

    fun showFlareon(){
        evoChart2.visibility = View.VISIBLE
        Glide.with(this).load(imageBaseURL + "136" + ".png").into(evoChart2)
        evolveOption2.text = "Flareon"
        evolveOption2.text = ""
        evoChart2.visibility = View.GONE
    }

So I would want evoChart2 (which is one of the three imageviews I have) to show Vaporeon for 3 seconds, then Jolteon for 3 seconds, then Flareon for 3 seconds, and then Vaporeon again for 3 seconds, Jolteon, Flareon etc.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • since you are new you should use handlers or countdown timers, I say use 3 handlers and nest them it will be easy for you https://stackoverflow.com/a/56023812/8528047 – Pemba Tamang Sep 27 '19 at 10:32

1 Answers1

4

Since you're wanting a loop, I'd suggest using a list to make it easier to iterate and add more pokemon in future.

Create a Pokemon data class if you don't have one already.

data class Pokemon(val name: String, val imageUrl: String)

Create however many instances of this class as you need and add them to a list.

val pokemonList: List<Pokemon> = listOf(vaporeon, jolteon, flareon)

We'll also need to store the index of the current pokemon we're displaying

var currentIndex = 0

Next we'll create a Runnable and schedule it to execute every three seconds, you may want to do this in onResume.

val service = Executors.newSingleThreadScheduledExecutor()
service.scheduleAtFixedRate({ displayPokemon() }, 0, 3, TimeUnit.SECONDS)

Now create the displayPokemon function that is going to be called every 3 seconds.

fun displayPokemon() {
}

Inside this function we need to know what the next pokemon we're displaying is, based on the current pokemon.

val next = currentIndex + 1
if (next >= pokemonList.size) {
     // we're at the end, go back to 0
     currentIndex = 0
} else {
     currentIndex = next
}

val pokemon = pokemonList[currentIndex]

Now that we have the next pokemon to display we can use it to populate the view

evoChart2.visibility = View.VISIBLE
Glide.with(this).load(pokemon.imageUrl).into(evoChart2)
evolveOption2.text = pokemon.name
evolveOption2.text = ""
evoChart2.visibility = View.GONE

Finally, we don't want this to happen when the Activity/Fragment is in the background so we add the following code to onPause

service.shutdown()
Scott Cooper
  • 2,779
  • 2
  • 23
  • 29
  • Thanks! I am going to try that out immediately! – frankhardenberg Sep 27 '19 at 11:04
  • could you tell me how to implement the Runnable? I was already trying to figure that out before I was asking this question. It's just somehow really annoying for me to learn Kotlin stuff as I only know C# so far, which was way more clear for me. I can't figure out what to do so I will also post a link with my code from the Activity I am working in right now. https://hastebin.com/ugawaxotiq.cpp – frankhardenberg Sep 27 '19 at 11:36
  • Which bit are you struggling with? From looking at your code it seems hardcoded to display a specific pokemon, you should aim to make it generic enough so that it can display any pokemon you tell it to. That way the code above will work – Scott Cooper Sep 27 '19 at 11:57
  • I just can't seem to figure out how to initialize 3 handlers, timers or runnables in kotlin which I could then use to fire and quit so that the images switch each couple of seconds. I can send you the whole project file, it's generic for the pokedex but since it is just one pokémon, and one project which will not be expanded I figured I could make an easier solution for these 4 pokémon. I am loading the images through a recyclerview, same for the names, then I've got a separate layout for the information page about these pokémon and that's where I want to add the imagevw w 3 diff pokemon to – frankhardenberg Sep 27 '19 at 12:02
  • btw I load the pokémon in through a JSON file – frankhardenberg Sep 27 '19 at 12:04
  • If you upload the project to Github I'll check it out and show you how I would do it, I don't think you need three Runnables, just one that fires every 3 seconds. – Scott Cooper Sep 27 '19 at 13:00
  • Never mind guys I fixed it. Thanks for all the help! – frankhardenberg Sep 27 '19 at 13:15
  • The big chunk of logic to increment `currentIndex` could be simplified to `currentIndex = (currentIndex + 1) % pokemonList.size` – Tenfour04 Sep 27 '19 at 13:35
  • My solution was to create two functions called drawEeveelution & showNextEeveelution where I would loop the eevee to 2 and when it's 2 set it back to 0 and so on. And in drawEeveelution I did a when loop and when currentEeveelution (which was incremented in showNextEeveelution) was 0 I would set the values for the Vaporeon, when it was 1 I would set the values for the Jolteon and 2 for the Flareon. Now I just need to make sure (I used a handler) that my app doesn't crash on pressing the next/previous or default back button from android. – frankhardenberg Sep 27 '19 at 13:54