2016-12-13 4 views
0

Я создал простую викторину. В этом я хочу получить истинное значение checkbox. есть три вопроса: checkbox. Теперь, как получить правильное значение выбранного флажка, используя ArrayList. Поэтому, когда несколько ответов выбрано пользователем, мне нужно сохранить их в arraylist и вам нужно сравнить выбранные ответы с данными в базе данных. В моем коде я сохраняю выбранный идентификатор значения один за другим, но за один раз он сохраняет только одно значение. Как сохранить все выбранные checkbox id в моем arraylist?Как сохранить выделенные флажки в списке массивов в android

Мой код:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<LinearLayout 
    android:id="@+id/activity_main" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/background" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.pujadudhat.quizapp.MainActivity"> 

    <TextView 
     android:id="@+id/tv_que1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/que_1" /> 

    <RadioGroup 
     android:id="@+id/rg_que1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <RadioButton 
      android:id="@+id/rb_opt1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:onClick="questionOne" 
      android:text="@string/rb_opt1" /> 

     <RadioButton 
      android:id="@+id/rb_opt2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="questionOne" 
      android:text="@string/rb_opt2" /> 

     <RadioButton 
      android:id="@+id/rb_opt3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="questionOne" 
      android:text="@string/rb_opt3" /> 
    </RadioGroup> 

    <TextView 
     android:id="@+id/tv_que2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="@dimen/activity_horizontal_margin" 
     android:text="@string/que_2" /> 

    <RadioGroup 
     android:id="@+id/rg_que2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <RadioButton 
      android:id="@+id/rb_opt1_2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:onClick="questionTwo" 
      android:text="@string/rb_opt1_2" /> 

     <RadioButton 
      android:id="@+id/rb_opt2_2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="questionTwo" 
      android:text="@string/rb_opt2_2" /> 

     <RadioButton 
      android:id="@+id/rb_opt3_2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="questionTwo" 
      android:text="@string/rb_opt3_2" /> 
    </RadioGroup> 

    <TextView 
     android:id="@+id/tv_que3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="@dimen/activity_horizontal_margin" 
     android:text="@string/que_3" /> 

    <RadioGroup 
     android:id="@+id/rg_que3" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <RadioButton 
      android:id="@+id/rb_opt1_3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:onClick="questionThree" 
      android:text="@string/rb_opt1_3" /> 

     <RadioButton 
      android:id="@+id/rb_opt2_3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="questionThree" 
      android:text="@string/rb_opt2_3" /> 

     <RadioButton 
      android:id="@+id/rb_opt3_3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="questionThree" 
      android:text="@string/rb_opt3_3" /> 
    </RadioGroup> 

    <TextView 
     android:id="@+id/tv_que4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="@dimen/activity_horizontal_margin" 
     android:text="@string/que_4" /> 

    <CheckBox 
     android:id="@+id/cb_prime" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="questionFour" 
     android:text="@string/cb_opt1_4" /> 

    <CheckBox 
     android:id="@+id/cb_prime2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="questionFour" 
     android:text="@string/cb_opt2_4" /> 

    <CheckBox 
     android:id="@+id/cb_prime3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="questionFour" 
     android:text="@string/cb_opt3_4" /> 

    <TextView 
     android:id="@+id/tv_que5" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="@dimen/activity_horizontal_margin" 
     android:text="@string/que_5" /> 

    <CheckBox 
     android:id="@+id/cb_composite" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="questionFive" 
     android:text="@string/cb_opt1_5" /> 

    <CheckBox 
     android:id="@+id/cb_composite2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="questionFive" 
     android:text="@string/cb_opt2_5" /> 

    <CheckBox 
     android:id="@+id/cb_composite3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="questionFive" 
     android:text="@string/cb_opt3_5" /> 

    <TextView 
     android:id="@+id/tv_que6" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="@dimen/activity_horizontal_margin" 
     android:text="@string/que_6" /> 

    <CheckBox 
     android:id="@+id/cb_multiple" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="questionSix" 
     android:text="@string/cb_opt1_6" /> 

    <CheckBox 
     android:id="@+id/cb_multiple2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="questionSix" 
     android:text="@string/cb_opt2_6" /> 

    <CheckBox 
     android:id="@+id/cb_multiple3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="questionSix" 
     android:text="@string/cb_opt3_6" /> 

    <TextView 
     android:id="@+id/tv_logo" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="@dimen/activity_horizontal_margin" 
     android:text="@string/que_7" /> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="@dimen/activity_horizontal_margin" 
     android:hint="@string/hint" /> 

    <ImageView 
     android:layout_width="250dp" 
     android:layout_height="250dp" 
     android:src="@drawable/rolex_logo" /> 

    <TextView 
     android:id="@+id/tv_score" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="@dimen/activity_horizontal_margin" 
     android:orientation="horizontal"> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:layout_weight="1" 
      android:onClick="calsulateScore" 
      android:text="@string/btn_text" /> 



     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight="1" 
      android:onClick="resetScore" 
      android:text="@string/btn_reset" /> 
    </LinearLayout> 
</LinearLayout> 

MainActivity.java

package com.pujadudhat.quizapp; 

import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.RadioButton; 
import android.widget.Toast; 

public class MainActivity extends AppCompatActivity { 

private int myScore = 0; 
boolean que1; 
boolean que2; 
boolean que3; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 

/** 
* Get Answer for que. #1 
*/ 

public void questionOne(View view) { 

    RadioButton opt1RadioButton = (RadioButton) findViewById(R.id.rb_opt1); 
    que1 = opt1RadioButton.isChecked(); 

    if (opt1RadioButton.isChecked()) { 

     myScore = myScore + 1; 
     Toast.makeText(getApplicationContext(), "Right!", Toast.LENGTH_SHORT).show(); 

    } else { 
     Toast.makeText(getApplicationContext(), "Wrong!", Toast.LENGTH_SHORT).show(); 
    } 
} 

public void questionTwo(View view){ 
    RadioButton opt2RadioButton = (RadioButton) findViewById(R.id.rb_opt2_2); 
    que2 = opt2RadioButton.isChecked(); 

    if(opt2RadioButton.isChecked()){ 

     myScore = myScore + 1; 
     Toast.makeText(this, "Right!", Toast.LENGTH_SHORT).show(); 

    } else { 
     Toast.makeText(this, "Wrong!", Toast.LENGTH_SHORT).show(); 
    } 
} 

public void questionThree(View view){ 
    RadioButton opt2RadioButton = (RadioButton) findViewById(R.id.rb_opt2_3); 
    que3 = opt2RadioButton.isChecked(); 

    if(opt2RadioButton.isChecked()){ 

     myScore = myScore + 1; 
     Toast.makeText(this, "Right!", Toast.LENGTH_SHORT).show(); 

    } else { 
     Toast.makeText(this, "Wrong!", Toast.LENGTH_SHORT).show(); 
    } 
} 

} 

Пожалуйста, помогите мне решить эту проблему

Спасибо

+0

почему не и использовать переключатель, только одна кнопка выбрана в то время, и легко для вас у, чтобы проверить его правильный ответ или нет –

+0

сначала сделать все вещи динамически в макете вы добавили к 3 вопрос. Используйте view pager или recycle view, и через позицию вы можете управлять всеми вещами – Pavya

+0

Я уже сделал с логикой переключателя. Теперь для checkbox мне нужно объявить каждый флажок с идентификатором, а не делать это, я хочу сохранить ссылку в списке массивов и получить ее. Как это сделать?? –

ответ

0

вы можете попробовать создать пользовательские классы,

public class Answer{ 
private int questionNumber; 
private boolean answer; 
    public Answer(int questionNumber,boolean answer) 
    { 
     this.questionNumber = questionNumber; 
     this,.answer=answer 
    } 
    public void setQuestionNumber(int questionNumber){this.questionNumber=questionNumber;} 
    public void setAnswer(boolean answer){this.answer=answer;} 

    public int getQuestionNumber(){return this.questionNumber;} 
    public boolean getAnswer(){return this.answer;} 

} 

и применить к mainActivity:

public class MainActivity extends AppCompatActivity { 
    private Answer _answer; 
    private List<Answer> _listAnswer = new ArrayList<>(); 

public void questionOne(View view) { 

    RadioButton opt1RadioButton = (RadioButton) findViewById(R.id.rb_opt1); 
    que1 = opt1RadioButton.isChecked(); 

    if (opt1RadioButton.isChecked()) { 

     _answer = new Answer(1,true); 
     myScore = myScore + 1; 
     Toast.makeText(getApplicationContext(), "Right!", Toast.LENGTH_SHORT).show(); 

    } else { 
     _answer = new Answer(1,false); 
     Toast.makeText(getApplicationContext(), "Wrong!", Toast.LENGTH_SHORT).show(); 
    } 
//to store the answer into list 
    _listAnswer.add(_answer); 
} 

сделать остальное на вопрос 2,3 ... общий вопрос вы имеете

и для доступа к списку вы можете попробовать:

for(int i=0;i<_listAnswer.size();i++) 
{ 
    String answerValue = if(_listAnswer.get(i).getAnswer()) ? "Right" : "Wrong" 

    if(_listAnswer.get(i).getAnswer()){ myScore++; } 
    Toast.makeText(getApplicationContext(), "Question Number : " + _listAnswer.get(i).getQuestionNumber() + " is " + answerValue); 
} 

надеюсь, что это поможет

+0

Позвольте мне попробовать и запустить. Спасибо –

+0

вы можете удалить myScore + = 1 в каждом классе вопросов и просто поместить их в Looping для, его легче отслеживать. Я просто отредактировал свой ответ. –

Смежные вопросы