added button for save and edit

This commit is contained in:
oxidiert
2025-08-04 15:21:17 +02:00
parent e22ff56ab1
commit 2cf9faed38
5 changed files with 110 additions and 64 deletions

View File

@ -19,7 +19,6 @@ class HandlerRadioQuestion(
private lateinit var layout: View
private lateinit var question: QuestionItem.RadioQuestion
// Bind the question data to the view
override fun bind(layout: View, question: QuestionItem) {
this.layout = layout
this.question = question as QuestionItem.RadioQuestion
@ -28,17 +27,14 @@ class HandlerRadioQuestion(
val questionTextView = layout.findViewById<TextView>(R.id.textView)
val questionTitle = layout.findViewById<TextView>(R.id.question)
// Set question text and optional text key
questionTextView.text = question.textKey?.let { LanguageManager.getText(languageID, it) } ?: ""
questionTitle.text = question.question?.let {
Html.fromHtml(LanguageManager.getText(languageID, it), Html.FROM_HTML_MODE_LEGACY)
} ?: ""
// Clear previous radio buttons if any
radioGroup.removeAllViews()
// Dynamically create radio buttons based on options
question.options.forEach { option ->
val radioButton = RadioButton(context).apply {
text = LanguageManager.getText(languageID, option.key)
@ -55,10 +51,8 @@ class HandlerRadioQuestion(
radioGroup.addView(radioButton)
}
// Restore previous answer if one was saved
restorePreviousAnswer(radioGroup)
// Handle Next button click
layout.findViewById<Button>(R.id.Qnext).setOnClickListener {
if (validate()) {
saveAnswer()
@ -67,7 +61,6 @@ class HandlerRadioQuestion(
val selectedRadioButton = layout.findViewById<RadioButton>(selectedId)
val selectedKey = selectedRadioButton.tag.toString()
// Check if there is a specific next question ID
val nextId = question.options.find { it.key == selectedKey }?.nextQuestionId
if (!nextId.isNullOrEmpty()) {
@ -80,13 +73,11 @@ class HandlerRadioQuestion(
}
}
// Handle Previous button click
layout.findViewById<Button>(R.id.Qprev).setOnClickListener {
goToPreviousQuestion()
}
}
// Restore previously saved answer (if exists)
private fun restorePreviousAnswer(radioGroup: RadioGroup) {
question.question?.let { questionKey ->
val savedAnswer = answers[questionKey] as? String
@ -102,12 +93,10 @@ class HandlerRadioQuestion(
}
}
// Validate if any radio button has been selected
override fun validate(): Boolean {
return layout.findViewById<RadioGroup>(R.id.RadioGroup).checkedRadioButtonId != -1
}
// Save selected answer and update points list
override fun saveAnswer() {
val radioGroup = layout.findViewById<RadioGroup>(R.id.RadioGroup)
val selectedId = radioGroup.checkedRadioButtonId
@ -116,17 +105,13 @@ class HandlerRadioQuestion(
question.question?.let { questionKey ->
// Alte Antwort und Punkt ermitteln
val oldAnswerKey = answers[questionKey] as? String
val oldPoint = oldAnswerKey?.let { question.pointsMap?.get(it) } ?: 0
// Alten Punkt entfernen, falls vorhanden
points.remove(oldPoint)
// Neue Antwort speichern
answers[questionKey] = answerKey
// Neuen Punkt ermitteln und hinzufügen
val newPoint = question.pointsMap?.get(answerKey) ?: 0
points.add(newPoint)
}