2016-04-06 13 views
0

Ich muss alle ausgewählten Radio-Button-Tags und Gruppenname erhalten, nachdem Benutzer klicken auf Appbar-Schaltfläche übermitteln und speichern Sie es in einer Liste.Speichern Radiobutton Auswahl aus verschiedenen Gruppen zu einer Liste

So kann ich die Benutzer abgegeben vergleichen Antwortliste mit der Liste vom Server ..

Wenn i = „Answer_Checked“ Geprüft verwenden, Liste über geschrieben, wenn ich in Frage eines anderen Radiobutton klicken 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> 

Antwort im 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}] 

Frage im 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}] 
+0

Sie haben die Hauptfunktion 'vorgesehen Answer_Checked' . Auch ein praktikables Projekt wäre hilfreicher, um Ihnen eine Antwort zu geben, als eine zu erstellen. – Jerin

+0

Ich denke, ich löste es, indem ich ein Wörterbuch anstelle von List .. Hier ist, was ich getan habe http://pastebin.com/dbWbfQuP – Digi23

+0

Wenn es dann gelöst ist, ist das großartig. Bitte fügen Sie Ihre Antwort ein und markieren Sie diese als beantwortet für andere Referenzen. Nun Wörterbuch funktioniert wie in Liste Sie überprüfen möglicherweise nicht, ob Element bereits in der Liste vorhanden ist oder nicht, bevor Sie hinzufügen. Überprüfen Sie den Status Ihrer Antwort, wenn Sie bei der Navigation zurück oder vorwärts die ausgewählte Antwort beibehalten oder nicht. – Jerin

Antwort

0

Ich löste es Dictionary anstelle der Verwendung Liste

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(); 

    } 
Verwandte Themen