Ashish Gautam
Ashish Gautam

Reputation: 355

How can I select one option out of 4? I am building quiz app

  1. This is what I try , what i know .
  2. please give me a right approach so i can proceed.
data class OptionState(
    val option : Options = Options.OPTION1) {

    enum class Options(
        val optionSelected : Boolean = false) {
        OPTION1(false),
        OPTION2(false),
        OPTION3(false),
        OPTION4(false)
    }
}


@Composable
fun AvailOptions(
    optionState: OptionState ,
    onSelect : (OptionState.Options) -> Unit
) {
    val (optionButton , setOptionButton) = remember {
        mutableStateOf(
            OptionState.Options.values().indexOf(optionState.option)
        )
    }


    Column(
        modifier = Modifier,
        verticalArrangement = Arrangement.spacedBy(8.dp)
    ) {
    PossibleAnswer(optionText = "option  1" )
    PossibleAnswer(optionText = "option  2" )
    PossibleAnswer(optionText = "option  3" )
    PossibleAnswer(optionText = "option  4" )

    }
}

Upvotes: 2

Views: 488

Answers (2)

kirkadev
kirkadev

Reputation: 348

I suggest you make it flexible.

  1. To display answer options, use the RecyclerView list, for example, so you will not be tied to the number of answer options and their type, especially since you can use not just a String or Enum, but an object of a certain model, which may be different (text question, video question, etc. other, you can also put a property in the model that means the parity of this answer, there can be several correct options, etc.) This will allow you to download answer options from any repository (local or remote using a full backend or Firebase).

  2. In order to process the press, send an onClickListener to the adapter, so you will find out which option was selected and process this press in the ViewModel or Presenter without overload the adapter.

  3. If you have multiple answers, you can use SelectionTracker to track the selected items.

Upvotes: 3

Emanuel Garcia
Emanuel Garcia

Reputation: 335

Try to use RadioGroup, that will managed by Android itself. Not needed to create to much logic.

Upvotes: 0

Related Questions