added edit mode for string spinner
This commit is contained in:
@ -3,6 +3,7 @@ package com.dano.test1
|
||||
import android.content.Context
|
||||
import android.view.View
|
||||
import android.widget.*
|
||||
import kotlinx.coroutines.*
|
||||
|
||||
class HandlerStringSpinner(
|
||||
private val context: Context,
|
||||
@ -10,7 +11,8 @@ class HandlerStringSpinner(
|
||||
private val languageID: String,
|
||||
private val goToNextQuestion: () -> Unit,
|
||||
private val goToPreviousQuestion: () -> Unit,
|
||||
private val showToast: (String) -> Unit
|
||||
private val showToast: (String) -> Unit,
|
||||
private val questionnaireMeta: String // neu: Meta, damit wir dieselbe DB-ID bilden können wie im Radio-Handler
|
||||
) : QuestionHandler {
|
||||
|
||||
private lateinit var layout: View
|
||||
@ -31,10 +33,42 @@ class HandlerStringSpinner(
|
||||
|
||||
val options = buildOptionsList()
|
||||
|
||||
// Falls bereits im answers-Map ein Eintrag existiert, wird dieser als gespeicherte Auswahl übergeben
|
||||
val savedSelection = question.question?.let { answers[it] as? String }
|
||||
|
||||
setupSpinner(spinner, options, savedSelection)
|
||||
|
||||
// Wenn noch kein Eintrag im answers-Map existiert: DB abfragen (wie im Radio-Handler)
|
||||
val answerMapKey = question.question ?: (question.id ?: "")
|
||||
if (answerMapKey.isNotBlank() && !answers.containsKey(answerMapKey)) {
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
try {
|
||||
val clientCode = GlobalValues.LAST_CLIENT_CODE
|
||||
if (clientCode.isNullOrBlank()) return@launch
|
||||
|
||||
val allAnswersForClient = MyApp.database.answerDao().getAnswersForClient(clientCode)
|
||||
val myQuestionId = questionnaireMeta + "-" + question.question
|
||||
val dbAnswer = allAnswersForClient.find { it.questionId == myQuestionId }?.answerValue
|
||||
|
||||
if (!dbAnswer.isNullOrBlank()) {
|
||||
withContext(Dispatchers.Main) {
|
||||
// Falls ein alter Eintrag existiert (theoretisch), wird er überschrieben
|
||||
answers[answerMapKey] = dbAnswer
|
||||
|
||||
// Auswahl im Spinner setzen, falls die Option vorhanden ist
|
||||
val index = options.indexOf(dbAnswer)
|
||||
if (index >= 0) {
|
||||
spinner.setSelection(index)
|
||||
}
|
||||
// falls dbAnswer nicht in options ist: nichts machen (kann optional geloggt werden)
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
layout.findViewById<Button>(R.id.Qnext).setOnClickListener {
|
||||
if (validate()) {
|
||||
saveAnswer()
|
||||
|
||||
@ -161,7 +161,7 @@ abstract class QuestionnaireBase<T> {
|
||||
is QuestionItem.ValueSpinnerQuestion -> HandlerValueSpinner(context, answers, languageID, ::goToNextQuestion, ::goToPreviousQuestion, ::goToQuestionById, ::showToast)
|
||||
is QuestionItem.GlassScaleQuestion -> HandlerGlassScaleQuestion(context, answers, points, languageID, ::goToNextQuestion, ::goToPreviousQuestion, ::showToast)
|
||||
is QuestionItem.ClientNotSigned -> HandlerClientNotSigned(answers, languageID, ::goToNextQuestion, ::goToPreviousQuestion, ::showToast)
|
||||
is QuestionItem.StringSpinnerQuestion -> HandlerStringSpinner(context, answers, languageID, ::goToNextQuestion, ::goToPreviousQuestion, ::showToast)
|
||||
is QuestionItem.StringSpinnerQuestion -> HandlerStringSpinner(context, answers, languageID, ::goToNextQuestion, ::goToPreviousQuestion, ::showToast, questionnaireMeta.id)
|
||||
is QuestionItem.MultiCheckboxQuestion -> HandlerMultiCheckboxQuestion(context, answers, points, languageID, ::goToNextQuestion, ::goToPreviousQuestion, ::showToast)
|
||||
is QuestionItem.LastPage -> HandlerLastPage(
|
||||
answers, languageID, ::goToNextQuestion, ::goToPreviousQuestion
|
||||
|
||||
Reference in New Issue
Block a user