vendredi 10 juillet 2020

Display a random layout in kotlin

I am trying to create an app that display random challenges in different levels of difficulty. I didn't used fragments but i actually created a layout that correspond to every challenge. In my app, i am displaying as MainActivity the different levels of difficulty which are represented by buttons. Each of those level button create an intent to an introductory page to the level which also contains a button at the bottom that should randomly select one of the layouts(categorized as part of this level) and display it to the user. My problem is that i don't know the code to do this kind of selection and previous answers didn't worked very well.

I tried to do this based on the answers:

 private val SafeChallenges = listOf(
    DeclutterPhone::class,
    Drink2glasses::class,
    TodoList::class
)

private fun startRandomActivity() {
    startActivity(Intent(this, SafeChallenges.random().java))
   
}   

The button didn't responded. I created 3 classes to have each one with a setContentView to a specific layout(challenge)

I also tried this type of loop but my button doesn't respond to it.

 override fun onClick(view: View){
  Log.d(TAG,"onclick: called")
    category12_challenge_button.setOnClickListener {
        Log.d(TAG,"button clicked")
        val myRandomChallenges = Random.nextInt(1..3)

        when(myRandomChallenges){
            1->startActivity(Intent(this,DeclutterPhone::class.java))
            2->startActivity(Intent(this,Drink2glasses::class.java))
            3->startActivity(Intent(this,TodoList::class.java))
            else -> IllegalArgumentException("unknown layout")
        }
    }

Could you tell me where I missed my point?




Aucun commentaire:

Enregistrer un commentaire