简体   繁体   English

Android Studio(Java)未按顺序接收正确的按钮值

[英]Android Studio (Java) not receiving sequentially correct Button values

I am currently working on a game where players are supposed to solve for X + Y with selection of 3 buttons (1 of which is answer) and a "Next" button to go to the next question (by replacing values of components into the next question's).我目前正在开发一个游戏,玩家应该通过选择 3 个按钮(其中一个是答案)和一个“下一步”按钮来解决下一个问题(通过将组件的值替换为下一个问题)。 However, I am not able to tally the answers because my tested selection (Button) is always testing the values of the next question但是,我无法统计答案,因为我的测试选项(按钮)总是在测试下一个问题的值

To give a little context,给一点背景,
questionCategories is an ArrayList of ArrayList with different levels questionCategories是一个不同级别的 ArrayList 的 ArrayList
eg questionCategories.get(selectedLevel) returns ArrayList of different arithmetics例如questionCategories.get(selectedLevel)返回不同算法的 ArrayList

jumbleOptions populates 3 buttons with correct and incorrect answers which are housed in options jumbleOptions使用选项中的正确和错误答案填充 3 个按钮

I'm having a situation where when answering Question 1, the shown option values belongs to Question 1, but the tested values (in test) are from Question 2.我遇到的情况是,在回答问题 1 时,显示的选项值属于问题 1,但测试值(在测试中)来自问题 2。

eg例如
Qn1: 3 + 5 - A: 2, B: 8, C: 4 Qn1: 3 + 5 - A: 2, B: 8, C: 4
Qn2: 8 + 2 - A: 10, B: 11, C:12 Qn2: 8 + 2 - A: 10, B: 11, C:12
When I attempt Qn1 on B and click "Next" to go to the next question (and test for correctness), Instead of testing B:8 my program ends up testing B: 11 instead.当我在 B 上尝试 Qn1 并单击“下一步”转到下一个问题(并测试正确性)时,我的程序最终测试了B:11,而不是测试B:8 I have tried to Log the captured values of each Button and Question's answer but they are all correct and I am not sure where did I go wrong.我试图记录每个按钮的捕获值和问题的答案,但它们都是正确的,我不确定我哪里出错了。

The code snippet is within onViewCreated , the first question is initialized to display because of placeholder text in the original element.代码片段在onViewCreated 中,第一个问题被初始化为显示,因为原始元素中的占位符文本。 I'm sorry if the code snippet does not make much sense, trying to keep it short so it's (hopefully) more readable.如果代码片段没有多大意义,我很抱歉,尽量保持简短,以便(希望)更具可读性。

// Initialize first question to display
questionNumber.setText("Question " + String.valueOf(qnNumDisplay));
questionTitle.setText(((questionCategories.get(selectedLevel)).get(0)).toString());
jumbleOptions(((questionCategories.get(selectedLevel)).get(0)));

do {
    nextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Question q = (questionCategories.get(selectedLevel)).get(numQnAnswered);
            // Increment question number related variable
            numQnAnswered++;
            // Replace displayable elements with next question
            //Question Number
            questionNumber.setText("Question " + String.valueOf(numQnAnswered));

            questionTitle.setText(q.toString());
            jumbleOptions(((questionCategories.get(selectedLevel)).get(numQnAnswered)));

            boolean test = checkAnswer(String.valueOf(q.getAnswer()),
                options.get(SELECTED_OPTION).getText().toString());
            Log.d("Answer Correctness", String.valueOf(test));
        }
    });
    counter++;
} while (counter < NUMBER_OF_QNS);

Let's simulate the scenario: First question is set up manually, and next question is loaded based on button click.让我们模拟一下场景:第一个问题是手动设置的,下一个问题是基于按钮点击加载的。 I assume numQnAnswered is 0 and you're getting the answer from your Question object of numQnAnswered th position, right?我假设numQnAnswered0并且您从numQnAnswered th 位置的Question对象中得到答案,对吗?

final Question q = (questionCategories.get(selectedLevel)).get(numQnAnswered);

As your codes say so.正如您的代码所说。 Notice here, carefully:注意这里,仔细:

// Increment question number related variable
numQnAnswered++;

Now the problem however is, the answer is validated inside button click, I think you've forgot the fact that the first question was set up manually... & before checking the answer for that one you've incremented the numQnAnswered variable, thus your question is one step behind your answer.然而,现在的问题是,答案是在按钮点击内验证的,我想你已经忘记了第一个问题是手动设置的事实...... & 在检查那个问题的答案之前,你已经增加了numQnAnswered变量,因此你的问题比你的答案落后一步。 Hope it make sense.希望这是有道理的。

Just decrement the numQnAnswered variable after the validation of current question.只需在验证当前问题后递减numQnAnswered变量即可。

// Initialize first question to display
questionNumber.setText("Question " + String.valueOf(qnNumDisplay));
questionTitle.setText(((questionCategories.get(selectedLevel)).get(0)).toString());
jumbleOptions(((questionCategories.get(selectedLevel)).get(0)));

do {
    nextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Question q = (questionCategories.get(selectedLevel)).get(numQnAnswered);
            // first we need to check answer of previous question 
            boolean test = checkAnswer(String.valueOf(q.getAnswer()),
                options.get(SELECTED_OPTION).getText().toString());
            Log.d("Answer Correctness", String.valueOf(test));

            // Increment question number related variable
            numQnAnswered++;
            // Replace displayable elements with next question
            //Question Number
            questionNumber.setText("Question " + String.valueOf(numQnAnswered));

            questionTitle.setText(q.toString());
            jumbleOptions(((questionCategories.get(selectedLevel)).get(numQnAnswered)));


        }
    });
    counter++;
} while (counter < NUMBER_OF_QNS);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM