I'm new to coding in kotlin, and i'm kinda stuck. This is my first real project, and is an android app, where you can play "Never have i ever". I've just tried hopelessly, and i'm not sure how to get a random question from my .txt file where i've got my questions. I was thinking that it would be best to generate a random number, use the random number as a "line number", and then output it to my textview. But i really don't know how. This is just a silly attempt, but i would like it to work
import android.os.Bundle
import android.util.Log.d
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_gamingpage.*
import kotlin.random.Random
import java.io.File
class Gamingpage : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_spilleside)
//actionbar
val actionbar = supportActionBar
//set actionbar title
actionbar!!.title = "New Activity"
//set back button
actionbar.setDisplayHomeAsUpEnabled(true)
actionbar.setDisplayHomeAsUpEnabled(true)
bigbutton.setOnClickListener {
// Debug
d("Console: ", "The next button was pressed")
// A random value from 1 to 10, to make sure that every question is different. In the future
// it will become more than just 1-10
val randomValues = List(1) { Random.nextInt(0, 10) }
// More debug
d("Console", " $randomValues")
// Value of question, which is the random line number of my txt file with questions
// It's also here where i'm stuck, and my app crashes
val spm = File("app/src/main/spm_liste/question_list.txt").useLines(randomValues)
// print used as debug
println(spm)
// My textview where the random question will be viewed
question.text = listOf(spm).toString()
}
}
override fun onSupportNavigateUp(): Boolean {
onBackPressed()
return true
}
}
Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire