Method resetQuiz
(Fig. 5.30) sets up and starts a quiz. Recall that the images for the game are stored in the app’s assets
folder. To access this folder’s contents, the method gets the app’s AssetManager
(line 134) by calling the parent Activity
’s getAssets method. Next, line 135 clears the fileNameList
to prepare to load image file names for only the enabled geographical regions. Lines 140–147 iterate through all the enabled world regions. For each, we use the AssetManager
’s list
method (line 143) to get an array of the flag image file names, which we store in the String
array paths
. Lines 145–146 remove the .png
extension from each file name and place the names in the fileNameList
. Asset-Manager
’s list
method throws IOException
s, which are checked exceptions (so you must catch or declare the exception). If an exception occurs because the app is unable to access the assets
folder, lines 149–152 catch the exception and log it for debugging purposes with Android’s built-in logging mechanism. Log static
method e is used to log error messages. You can see the complete list of Log
methods at
Next, lines 154–156 reset the counters for the number of correct guesses the user has made (correctAnswers
) and the total number of guesses the user has made (total-Guesses
) to 0 and clear the quizCountriesList
.
Lines 162–175 add FLAGS_IN_QUIZ
(10) randomly selected file names to the quiz-CountriesList
. We get the total number of flags, then randomly generate the index in the range 0 to one less than the number of flags. We use this index to select one image file name from fileNamesList
. If the quizCountriesList
does not already contain that file name, we add it to quizCountriesList
and increment the flagCounter
. We repeat this process until FLAGS_IN_QUIZ
unique file names have been selected. Then line 177 calls loadNextFlag
(Fig. 5.31) to load the quiz’s first flag.