Apache – Dynamically adding radiobuttongroup

apache-flexflex3

I'm trying to make a quiz in flex and am loading data from an xml file. For each question I want to create a radiobuttongroup so I can associate radio buttons to it. How can I accomplish that with actionscript? I can see that addChild method works for DisplayObjects and I presume that radiobuttongroup is not one because I'm receiving errors. How can I dynamically add radiobuttongroup with actionscript in flex application? Thanks.

Best Answer

If you add radio buttons to a FormItem, they are automatically grouped together. So, assuming your quiz uses a Flex Form for layout, you simply generate a FormItem for each question, add a button for each option to the FormItem, then add that FormItem to your main Form.

private function generateQuestions(questions:XML):void
{
    var form:Form = new Form();
    this.addChild(form);

    for each (var question:XML in questions.question)
    {
        var questionItem:FormItem = new FormItem();
        form.addChild(questionItem);
        questionItem.label = question.@text;

        for each (var option:XML in question.option)
        {
            var optionButton:RadioButton = new RadioButton();
            optionButton.label = option.@text;
            questionItem.addChild(optionButton);
        }
}
Related Topic