Updating the Controller Layer

In the previous chapter, there was not much happening in GeoQuiz’s one controller, MainActivity. It displayed the layout defined in activity_main.xml. It set listeners on two buttons and wired them to make toasts.

Now that you have multiple questions to retrieve and display, MainActivity will have to work harder to tie GeoQuiz’s model and view layers together.

Open MainActivity.kt. Create a list of Question objects and an index for the list.

Listing 2.5  Adding a Question list (MainActivity.kt)

class MainActivity : AppCompatActivity() {

    private lateinit var trueButton: Button
    private lateinit var falseButton: Button

    private val questionBank = listOf(
            Question(R.string.question_australia, true), ...

Get Android Programming: The Big Nerd Ranch Guide, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.