0

I need to get all selected radio button tags and group name after user clicks on Appbar button submit and store it in a List.

So i can compare the user submitted answer list with the list from the server..

If i use Checked="Answer_Checked" , List is over written when i click another radio button in Question 2

public class RootObject
    {
        public RootObject(int id, string question, int qno, int qcount)
        {
            this.id = id;
            this.question = question;
            this.qcount = qcount;
            this.qno = qno;
        }
        public int id { get; set; }
        public string question { get; set; }
        public int qcount { get; set; }
        public int qno { get; set; }
        public int time { get; set; }
    }

public class AnswerObject
    {
        public AnswerObject(int question_id, int answer_id, string answer, int is_right_option)
        {
            this.question_id = question_id;
            this.answer_id = answer_id;
            this.answer = answer;
            this.is_right_option = is_right_option;
        }
        public int question_id { get; set; }
        public int answer_id { get; set; }
        public string answer { get; set; }
        public int is_right_option { get; set; }
    }

    public class Question
    {
        public string QuestionName { get; set; }
        public int qcount { get; set; }
        public int qno { get; set; }

        public ObservableCollection<Option> options { get; set; }
    }

    public class Option
    {
        public string QuestionAnswer { get; set; }
        public string groupname { get; set; }
        public int IsCorrect { get; set; }
    }

C# Coding

    var result1 = await response1.Content.ReadAsStringAsync();
            var objResponse1 = JsonConvert.DeserializeObject<List<RootObject>>(result1);

            var result2 = await response2.Content.ReadAsStringAsync();
            var objResponse2 = JsonConvert.DeserializeObject<List<AnswerObject>>(result2);

            for (int i = 0; i < objResponse1.LongCount(); i++)
            {
                ObservableCollection<Option> options1 = new ObservableCollection<Option>();
                for (int j = 0; j < objResponse2.LongCount(); j++)
                {
                    if (objResponse1[i].id == objResponse2[j].question_id)
                    {
                        options1.Add(new Option() { QuestionAnswer = objResponse2[j].answer, IsCorrect = objResponse2[j].is_right_option, groupname = objResponse2[j].question_id.ToString() });
                    }
                }
                questions.Add(new Question() { QuestionName = objResponse1[i].question, qno=i + 1, qcount =objResponse1.Count, options = options1 });
            }
            flipView.ItemsSource = questions;

XAML Coding

<FlipView x:Name="flipView" HorizontalAlignment="Left" VerticalAlignment="Top"  ItemsSource="{Binding}" Margin="0,35,0,0">
        <FlipView.ItemTemplate>
            <DataTemplate>
                <ListView Name="ItemData" SelectionMode="None" ItemsSource="{Binding}" >
                    <Grid x:Name="ContentPanel">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <TextBlock x:Name="Testing" Margin="10,20" FontSize="22.333">
                        <Run Text="{Binding qno}"/>
                        <Run Text="of"/>
                        <Run Text="{Binding qcount}"/>
                        </TextBlock>
                        <TextBlock x:Name="Question"   Text="{Binding QuestionName}" Margin="10,60" VerticalAlignment="Top" HorizontalAlignment="Left" FontSize="22.333" TextWrapping="Wrap"/>
                        <ListBox Grid.Row="1" Padding="0" Margin="10,-10"  ItemsSource="{Binding options}" Background="Transparent">

                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <RadioButton x:Name="Answer" GroupName="{Binding groupname}" Checked="Answer_Checked" Tag="{Binding IsCorrect}" Margin="10,2">
                                        <RadioButton.Content>
                                            <TextBlock Text="{Binding QuestionAnswer}" Foreground="White"/>
                                        </RadioButton.Content>
                                    </RadioButton>
                                                                        </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </Grid>
                </ListView>
            </DataTemplate>
        </FlipView.ItemTemplate>
    </FlipView>

Answer in JSON Format

[{"question_id":3,"answer_id":1,"answer":"10%","is_right_option":0},{"question_id":3,"answer_id":2,"answer":"10.25%","is_right_option":1},{"question_id":3,"answer_id":3,"answer":"10.5%","is_right_option":0},{"question_id":3,"answer_id":4,"answer":"None of these","is_right_option":0},{"question_id":4,"answer_id":5,"answer":"Rs. 2.04","is_right_option":1},{"question_id":4,"answer_id":6,"answer":"Rs. 3.06","is_right_option":0},{"question_id":4,"answer_id":7,"answer":"Rs. 4.80","is_right_option":0},{"question_id":4,"answer_id":8,"answer":"Rs. 8.30","is_right_option":0}]

Question in JSON Format

 [{"id":3,"question":"An automobile financier claims to be lending money at simple interest, but he includes the interest every six months for calculating the principal. If he is charging an interest of 10%, the effective rate of interest becomes: ","time":1},{"id":4,"question":"What is the difference between the compound interests on Rs. 5000 for 1 years at 4% per annum compounded yearly and half-yearly? ","time":1}]
Digi23
  • 1
  • 4
  • You havent provided the main function `Answer_Checked`. Also a workable project would be more helpful in giving you an answer than creating one. – Jerin Apr 06 '16 at 11:31
  • I think i solved it by creating a dictionary instead of List..Here is what i did http://pastebin.com/dbWbfQuP – Digi23 Apr 06 '16 at 11:43
  • If its solved then that is great. Please paste your answer and mark it as answered for others reference. Well dictionary worked as in List you might not be checking if item is already present in list or not before adding. Do check the state of your answer when on navigating back or forward the particular answer that you had selected is maintained or not. – Jerin Apr 06 '16 at 12:01
  • Sure, will post the code for reference. Yes answer state is maintained when moving back and forward in flipview. I got another problem though Listbox and Radiobutton is getting selected - Screenshot - http://i.imgur.com/VhqaWG6.png I need only one or Clicking the Listbox selecting the corresponding radio button. – Digi23 Apr 06 '16 at 12:11
  • you can have a look at this [answer](http://stackoverflow.com/a/8975226/2898399) You would need to override the default selection state of a Listbox. – Jerin Apr 06 '16 at 12:26
  • Thanks @Jerin It worked !:) – Digi23 Apr 06 '16 at 14:34

1 Answers1

0

I solved it using Dictionary instead of using List

private void Answer_Checked(object sender, RoutedEventArgs e) // Radio button click
    {
        var radio = sender as RadioButton;
        bool check = Convert.ToBoolean(radio.IsChecked);
        if(check)
        {
            Answer[Convert.ToInt16(radio.GroupName)] = Convert.ToInt16(radio.Tag);
        }
    }

    public async void Check_Result() // Evaluate result
    {
        foreach (KeyValuePair<int, int> count in Answer)
        {
            if (count.Value == 1)
            {
                result++;
            }
        }
        MessageDialog showresult = new MessageDialog(result.ToString());
        await showresult.ShowAsync();
        Frame.Navigate(typeof(MainPage), null);
    }


    public void TestSubmit_Click(object sender, RoutedEventArgs e) // AppBar button click
    {
        Check_Result();

    }
Digi23
  • 1
  • 4