如何在android中的数组列表中存储seleced复选框值

时间:2016-12-13 06:33:25

标签: android arraylist checkbox

我创建了简单的测验应用。我希望得到checkbox的真实价值。有三个checkbox个问题。现在,如何使用ArrayList获取所选复选框的正确值。因此,当用户选择多个答案时,我需要将它们存储在arraylist中,并且需要将所选答案与数据库中的答案进行比较。在我的代码中,我一个接一个地存储所选择的值id,但是一次只存储一个值。如何将所有选定的checkbox ID存储在我的arraylist

我的代码:

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

}

请帮我解决这个问题

谢谢

1 个答案:

答案 0 :(得分: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);
}

希望这会有所帮助