So, I am working on a quiz app. In the first activity screen, user is asked to select the "n" of questions that they would like to answer. Based on the user selection, n of questions are asked and a final score is displayed after user has answered n of questions.
How do I now create "n" of question screens/activities for the user to answer?
So, I would appreciate guidance on how to best approach this problem? I'm new to android studio and doing my best to learn.
My initial approach was to create as many activities as there are total possible questions but this seems rather tedious and a brute force method and does not completely work. For example if user selects 3 questions to answer but my 4th activity button says "next" instead of "finish".
CodePudding user response:
Can't understand Your Question.
CodePudding user response:
You are correct, it would be an absolute nightmare to create n activities for n questions. As per the description provided, you would need three activities. One for taking user input, a second activity for displaying questions, and one last activity for the final score display. Only one activity is needed for displaying questions because instead of creating new activities for each question and then setting the text for question and options in layout file, it would be better to create only one activity and just change the text of the question and options for each question programmatically as soon as the user clicks on next button.
To achieve this, You should first create a model class with all properties you need for a single question.
data class QuestionModel(val question:String, val option1: String, val option2:String, val option3:String)
Then, you should create a list of QuestionModel whose length is equal to the maximum number of questions the user is allowed to input for the test otherwise, you can show a toast message that the user must decrease the input number.
private val allQuestionModelList = mutableListOf<QuestionModel>()
private fun createAllQuestionModelList():List<QuestionModel>{
allQuestionModelList.add(QuestionModel("question 1?", "option 1 A", "option 1 B", "option 1 C"))
allQuestionModelList.add(QuestionModel("question 2?", "option 2 A", "option 2 B", "option 2 C"))
allQuestionModelList.add(QuestionModel("question 3?", "option 3 A", "option 3 B", "option 3 C"))
allQuestionModelList.add(QuestionModel("question 4?", "option 4 A", "option 4 B", "option 4 C"))
.
.
.
return allQuestionModelList
}
Then, you must extract the desired number of questions from all questions list using the user input number. You can use the shuffle() function to shuffle the questions list if needed.
val userSpecificQuestionModelList = createAllQuestionModelList().shuffled().take(userInput)
Maintain an index variable for the current question which is displayed from the user question list.
private var currentQstnIndex = 0
When user clicks on next button, the current index should increase by 1 and current texts for the question and options should be replaced by next question and option texts of the userSpecificQuestionModelList. If current index is equal to last index of userSpecificQuestionModelList that means, it is the last question and finishTest() function must be called. At the second last question, you must also change the text of next button to finish.
binding.nextButton.setOnClickListener {
if (currentQstnIndex == userSpecificQuestionModelList.size - 1) {
finishTest()
} else {
currentQstnIndex = 1
setQuestionOptionText()
if(currentQstnIndex == userSpecificQuestionModelList.size - 2) {
binding.nextButton.text = "Finish"
}
}
private fun finishTest(){
finish()
val intent = Intent(this, LastActivity::class.java)
startActivity(intent)
}
private fun setQuestionOptionText(){
binding.qstn.text = userSpecificQuestionModelList[currentQstnIndex].question
binding.op1.text = userSpecificQuestionModelList[currentQstnIndex].option1
binding.op2.text = userSpecificQuestionModelList[currentQstnIndex].option2
binding.op3.text = userSpecificQuestionModelList[currentQstnIndex].option3
}
The complete code for the question activity :
class SecondActivity : AppCompatActivity() {
private val allQuestionModelList = mutableListOf<QuestionModel>()
private var userSpecificQuestionModelList = listOf<QuestionModel>()
private val userInput = 3 // dummy user input fetched from first activity.
private var currentQstnIndex = 0
private lateinit var binding:ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
userSpecificQuestionModelList = createAllQuestionModelList().shuffled().take(userInput)
setQuestionOptionText()
binding.nextButton.setOnClickListener {
if (currentQstnIndex == userSpecificQuestionModelList.size - 1) {
finishTest()
} else {
currentQstnIndex = 1
setQuestionOptionText()
if(currentQstnIndex == userSpecificQuestionModelList.size - 2) {
binding.nextButton.text = "Finish"
}
}
}
}
private fun finishTest(){
finish()
val intent = Intent(this, LastActivity::class.java)
startActivity(intent)
}
private fun setQuestionOptionText(){
binding.qstn.text = userSpecificQuestionModelList[currentQstnIndex].question
binding.op1.text = userSpecificQuestionModelList[currentQstnIndex].option1
binding.op2.text = userSpecificQuestionModelList[currentQstnIndex].option2
binding.op3.text = userSpecificQuestionModelList[currentQstnIndex].option3
}
private fun createAllQuestionModelList():List<QuestionModel>{
allQuestionModelList.add(QuestionModel("question 1?", "option 1 A", "option 1 B", "option 1 C"))
allQuestionModelList.add(QuestionModel("question 2?", "option 2 A", "option 2 B", "option 2 C"))
allQuestionModelList.add(QuestionModel("question 3?", "option 3 A", "option 3 B", "option 3 C"))
allQuestionModelList.add(QuestionModel("question 4?", "option 4 A", "option 4 B", "option 4 C"))
return allQuestionModelList
}
}