如何更新所选 radioGroup/position 个单选按钮的分数?

How to update the score for every radioButton of the radioGroup/position the selected?

我有一个测验应用程序 sourcecode 在 recyclerView 中有多个 radioGroups,我希望每次 selected 某个位置(radioGroup)的正确 radioButton 时,它应该更新分数 correct++ 并将其发送到 activity,如下所示。

        @Override
        public void onClick(View view) {
            boolean checked = ((RadioButton) view).isChecked();

            if (checked) {
                int radioButtonID = mRadioGroup.getCheckedRadioButtonId();
                View radioButton = mRadioGroup.findViewById(radioButtonID);
                int selectedAnswerIndex = mRadioGroup.indexOfChild(radioButton);
                RadioButton r = (RadioButton) mRadioGroup.getChildAt(selectedAnswerIndex);
                String  selectedAnswer = r.getText().toString();

                int position = getAdapterPosition();
                Object object = mArrayList.get(position);
                String correctAnswer = ((Quiz) object).mCorrectAnswer;

                if (selectedAnswer.equals(correctAnswer)) {
                    correct++;
                    editor.putInt("score", correct);
                    editor.apply();
                }
            }
        }

这有效,但仅适用于一个 radioGroup,就像我 select 来自不同位置的另一个 radioButton 一样,分数 correct 始终为 1 可能是因为它在 [=14 之前重置为默认值=] 函数再次执行。

我可能的解决方案是循环 i <= arrayList.size() 以包含 if (checked) 以防止分数 correct 被重置为默认值 = 0,但我不知道将它放在哪里以及要包含的内容,因为用户从每个 radioGroup 中 select 并不是强制性的(除非是最简单的情况,这是一项要求)。

如何更新 radioGroup/position selected 的每个单选按钮的分数?

添加 for 循环解决了所有问题

        @Override
        public void onClick(View view) {
            boolean checked = ((RadioButton) view).isChecked();

            int position = getAdapterPosition();
            for (int i = 0; i <= position; i++) {
                if (checked) {
                    int radioButtonID = mRadioGroup.getCheckedRadioButtonId();
                    View radioButton = mRadioGroup.findViewById(radioButtonID);
                    int selectedAnswerIndex = mRadioGroup.indexOfChild(radioButton);
                    RadioButton r = (RadioButton) mRadioGroup.getChildAt(selectedAnswerIndex);
                    String selectedAnswer = r.getText().toString();

                    Object object = mArrayList.get(position);
                    String correctAnswer = ((Quiz) object).mCorrectAnswer;

                    if (selectedAnswer.equals(correctAnswer)) {
                        correct++;
                        editor.putInt("score", correct);
                        editor.apply();
                    }
                }
            }
        }