0

Here's the scenario:
enter image description here

I made some questions in one activity and every answer has a point(0 to 3). For example if the user select the first one, he gets 0 point, if he select the secound one he will get 1 point etc(2 point for the third one and 3 point for the forth).

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (checkedId == R.id.first_radiobutton) {
                totalPoint = totalPoint + 0;
            } else if (checkedId == R.id.secound_radiobutton) {
                totalPoint = totalPoint + 1;
            }else if (checkedId == R.id.third_radiobutton) {
                totalPoint = totalPoint + 2;
            }else if (checkedId == R.id.forth_radiobutton) {
                totalPoint = totalPoint + 3;
            }
        }
    });

I made totalPoint as global variable and initialize it as public static int totalPoint = 0; . The problem is that if the user has changed his mind and select another answer for the current question, the totalPoint variable will add the point to itself. For example imagine that if the user click on the third radioButton , the totalPoint variable will be 2, so if the user has changed his mind and select the forth radioButton, the totalPoint must be 3 but instead it will be 5(2+3). How can I avoid this?

Sina Amiri
  • 85
  • 1
  • 9
  • always there will be 4 buttons or it will dynamic? – Pavan May 30 '17 at 11:18
  • Try to save last selected position or radiogroupid (default = -1) of radiogroup and when you select new one try to remove the Points related to last selected position... – 9spl May 30 '17 at 11:20
  • @Pavan each question has 4 available answer. – Sina Amiri May 30 '17 at 11:22
  • why don't you add the values in `Next` button click? – SRB Bans May 30 '17 at 11:24
  • 1
    you have to store the points in an array(List). The index is the number of the question. If the user like to change his answer on the previews question you have to change the point in the array(List) at the index = the number of the question. – Paul May 30 '17 at 11:26

2 Answers2

0

declare one int array at class

 int arrValues[]={0,0,0,0}; ///size can be differ as per question length you can create dynamic too as per your requirement

and maintain on question counter or position int iPosition=0; ///handle it on on previous and next

then in setOnCheckedChangeListener add following code

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (checkedId == R.id.first_radiobutton) {
               arrValues[iPosition] = 0;
            } else if (checkedId == R.id.secound_radiobutton) {
                arrValues[iPosition] =  1;
            }else if (checkedId == R.id.third_radiobutton) {
               arrValues[iPosition]=  2;
            }else if (checkedId == R.id.forth_radiobutton) {
              arrValues[iPosition] =  3;
            }
        }
    });

and final step get addition of all value from array

Pavan
  • 5,016
  • 1
  • 26
  • 30
0

1. Use an array answers[] to store the answer points for specific question.

2. In onCheckedChanged(), check selected RadioButton and store allocated points under specific question by using the value of currentQuestion.

3. Calculate total points by adding the values of answers array.

Here is the working code:

public class MainActivity extends AppCompatActivity {

    RadioGroup radioGroup;

    int totalPoint = 0;
    int answers[] = new int[10]; // Required for 10 questions (0 to 9)
    int currentQuestion = 0;

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

        radioGroup = (RadioGroup) findViewById(R.id.radio_group);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                // Store points of specific question using position currentQuestion
                if (checkedId == R.id.first_radiobutton) {
                    answers[currentQuestion] = 0;
                } else if (checkedId == R.id.second_radiobutton) {
                    answers[currentQuestion] = 1;
                }else if (checkedId == R.id.third_radiobutton) {
                    answers[currentQuestion] = 2;
                }else if (checkedId == R.id.forth_radiobutton) {
                    answers[currentQuestion] = 3;
                }

                // Total
                totalPoint = getTotalPoint();
            }
        });
    }

    public int getTotalPoint() {

        int sum = 0;
        for (int i = 0; i < answers.length; i++)
            sum += answers[i];

        return sum;
    }
}
Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61