-1

Results.java 类中的 showResults() 方法的输出无法正常工作。琐事游戏循环遍历所有 10 个问题并显示供用户选择答案。第 10 题之后,会显示一个结果页面,其中包含正确和错误答案的结果。正确的问题将显示为绿色,不正确的问题将显示为红色。问题的颜色没有以正确的颜色显示。此外,变量“correctAnswers”和“wrongAnswers”不会在 QuestionView 类中递增。这可以从 Results.java 中的 showResults() 的输出中看出。

我已经在很多地方进行了调试,但似乎无法找到我搞砸的地方。

对此问题的帮助以及代码结构建议表示赞赏!先感谢您!

结果.java

public class Results extends Activity {

    QuestionView qv = new QuestionView();
    ArrayList<Question> queryList = qv.getQueries();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.resultsmain);

        Button homeBtn = (Button)findViewById(R.id.homeBtn);
        Button highscoresBtn = (Button)findViewById(R.id.highscoresBtn);

        homeBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                Intent intent1 = new Intent(Results.this, MainMenu.class);
                startActivity(intent1);
            }
        });

        highscoresBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                Intent intent2 = new Intent(Results.this, Highscores.class);
                startActivity(intent2);
            }
        });

        showResults();
    }

    public void showResults() {

        ArrayList<TextView> tList = new ArrayList<TextView>(10);

        TextView header = (TextView)findViewById(R.id.header);
        TextView q1 = (TextView)findViewById(R.id.q1);
        TextView q2 = (TextView)findViewById(R.id.q2);
        TextView q3 = (TextView)findViewById(R.id.q3);
        TextView q4 = (TextView)findViewById(R.id.q4);
        TextView q5 = (TextView)findViewById(R.id.q5);
        TextView q6 = (TextView)findViewById(R.id.q6);
        TextView q7 = (TextView)findViewById(R.id.q7);
        TextView q8 = (TextView)findViewById(R.id.q8);
        TextView q9 = (TextView)findViewById(R.id.q9);
        TextView q10 = (TextView)findViewById(R.id.q10);

        tList.add(q1);
        tList.add(q2);
        tList.add(q3);
        tList.add(q4);
        tList.add(q5);
        tList.add(q6);
        tList.add(q7);
        tList.add(q8);
        tList.add(q9);
        tList.add(q10);

        tList.get(0).setText(queryList.get(0).getQuery());

        if(queryList.get(0).getCorrectness() == true) {
            tList.get(1).setText("Changed to true");
        } else {
            tList.get(1).setText("DID NOT Change to true");
        }

        tList.get(2).setText(Integer.toString(qv.getCorrectAnswers()));
        tList.get(3).setText(Integer.toString(qv.getWrongAnswers()));

/*      for(int i = 0; i < 10; i++) {
            tList.get(i).setText(queryList.get(i).getQuery());
            if(queryList.get(i).getCorrectness() == true) {
                tList.get(i).setTextColor(Color.GREEN);
            } else {
                tList.get(i).setTextColor(Color.RED);
            }
        }*/
    }
}

QuestionView.java

public class QuestionView extends Activity {

    Quiz quiz = new Quiz();
    ArrayList<Question> queries = quiz.getRandom10();

    int correctAnswers = 3;
    int wrongAnswers = 3;

    int answer = 0;

    int i = 0;

    Button answer1;
    Button answer2;
    Button answer3;
    Button answer4;
    TextView question;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.questionviewmain);

        answer1 = (Button)findViewById(R.id.answer1);
        answer2 = (Button)findViewById(R.id.answer2);
        answer3 = (Button)findViewById(R.id.answer3);
        answer4 = (Button)findViewById(R.id.answer4);

        question = (TextView)findViewById(R.id.question);

        loadQuestion();
    }

    public void loadQuestion() {

        if(i == 9) {

            endQuiz();

        } else {

            answer = queries.get(i).getCorrectAnswer();

            question.setText(queries.get(i).getQuery());

            answer1.setText(queries.get(i).getA1());
            answer2.setText(queries.get(i).getA2());
            answer3.setText(queries.get(i).getA3());
            answer4.setText(queries.get(i).getA4());

            answer1.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    if(answer == 0) {
                        correctAnswers++;
                        queries.get(i).setSelectedAnswer(0);
                        queries.get(i).setCorrectness(true);
                        nextQuestion();
                    } else {
                        wrongAnswers++;
                        queries.get(i).setCorrectness(false);
                        nextQuestion();
                    }
                }
            });

            answer2.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    if(answer == 1) {
                        correctAnswers++;
                        queries.get(i).setSelectedAnswer(1);
                        queries.get(i).setCorrectness(true);
                        nextQuestion();
                    } else {
                        wrongAnswers++;
                        queries.get(i).setCorrectness(false);
                        nextQuestion();
                    }
                }
            });

            answer3.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    if(answer == 2) {
                        correctAnswers++;
                        queries.get(i).setSelectedAnswer(2);
                        queries.get(i).setCorrectness(true);
                        nextQuestion();
                    } else {
                        wrongAnswers++;
                        queries.get(i).setCorrectness(false);
                        nextQuestion();
                    }
                }
            });

            answer4.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    if(answer == 3) {
                        correctAnswers++;
                        queries.get(i).setSelectedAnswer(3);
                        queries.get(i).setCorrectness(true);
                        nextQuestion();
                    } else {
                        wrongAnswers++;
                        queries.get(i).setCorrectness(false);
                        nextQuestion();
                    }
                }
            });
        } 
    }

    public int getCorrectAnswers() { return correctAnswers; }
    public int getWrongAnswers() { return wrongAnswers; }

    public ArrayList<Question> getQueries() {
        return queries;
    }

    public void nextQuestion() {
        i++;
        loadQuestion();
    }

    public void endQuiz() {
        Intent intent = new Intent(QuestionView.this, Results.class);
        startActivity(intent);
    }
}

测验.java

public class Quiz {

    ArrayList<Question> qList = new ArrayList<Question>(100);
    ArrayList<Question> tenQs = new ArrayList<Question>(10);

    public Quiz() {

        qList.add(new Question("A", "B", "C", "D", 3, "Question 1?", 0, false));
        qList.add(new Question("A", "B", "C", "D", 3, "Question 2?", 0, false));
        qList.add(new Question("A", "B", "C", "D", 3, "Question 3?", 0, false));
        qList.add(new Question("A", "B", "C", "D", 3, "Question 4?", 0, false));
        qList.add(new Question("A", "B", "C", "D", 3, "Question 5?", 0, false));
        qList.add(new Question("A", "B", "C", "D", 3, "Question 6?", 0, false));
        qList.add(new Question("A", "B", "C", "D", 3, "Question 7?", 0, false));
        qList.add(new Question("A", "B", "C", "D", 3, "Question 8?", 0, false));
        qList.add(new Question("A", "B", "C", "D", 3, "Question 9?", 0, false));
        qList.add(new Question("A", "B", "C", "D", 3, "Question 10?", 0, true));
        qList.add(new Question("A", "B", "C", "D", 3, "Question 11?", 0, true));
        qList.add(new Question("A", "B", "C", "D", 3, "Question 12?", 0, true));
        qList.add(new Question("A", "B", "C", "D", 3, "Question 13?", 0, true));
        qList.add(new Question("A", "B", "C", "D", 3, "Question 14?", 0, true));
    }

    public ArrayList<Question> getRandom10() {
        Random r = new Random();
        for(int i = 0; i < 10; i++) {
            Question x = qList.get(r.nextInt(qList.size()));
            tenQs.add(x);
        }
        return tenQs;
    }
}

问题.java

public class Question {
    String a1;
    String a2;
    String a3;
    String a4;
    int correctAnswer;
    String query;
    int selectedAnswer;
    boolean correctness;

    public Question() {
    }

    public Question(String a1, String a2, String a3, String a4, int correctAnswer, String query, int selectedAnswer, boolean correctness) {
        this.a1 = a1;
        this.a2 = a2;
        this.a3 = a3;
        this.a4 = a4;
        this.correctAnswer = correctAnswer;
        this.query = query;
        this.selectedAnswer = selectedAnswer;
        this.correctness = correctness;
    }

    public String getA1() { return a1; }
    public String getA2() { return a2; }
    public String getA3() { return a3; }
    public String getA4() { return a4; }
    public String getQuery() { return query; }
    public int getCorrectAnswer() { return correctAnswer; }
    public boolean getCorrectness() { return correctness; }

    public void setSelectedAnswer(int newAnswer) {
        selectedAnswer = newAnswer;
    }

    public void setCorrectness(boolean newAnswer) {
        correctness = newAnswer;
    }
}
4

1 回答 1

3

QuestionView 类中的变量“correctAnswers”和“wrongAnswers”没有递增。这可以从 Results.java 中的 showResults() 的输出中看出。

一旦将 Activity 置于后台,就无法保证它不会在没有警告的情况下被销毁。您需要在 Intent 中传递答案计数:

public void endQuiz() {
    Intent intent = new Intent(QuestionView.this, Results.class);
    intent.putExtras("correct", correctAnswers);
    intent.putExtras("wrong", wrongAnswers);
    intent.putExtras("queries", queries); // You will need to make Question implement Parcelable 
    startActivity(intent);
}

然后在结果活动中读取这些值。否则,您将面临丢失此信息并获得默认值 3 的风险。(简而言之,没有任何问题correctAnswers++。)


你也有很多非常相似/重复的代码。例如,您可以在答案中使用一个 OnClickListener 并进行一些小的更改:

OnClickListener answerClick = new OnClickListener() {
    public void onClick(View v) {
        int index = (Integer) v.getTag();
        if(answer == index) {
            correctAnswers++;
            queries.get(i).setSelectedAnswer(index);
            queries.get(i).setCorrectness(true);
        } else {
            wrongAnswers++;
            queries.get(i).setCorrectness(false);
        }
        nextQuestion();
    }
};

answer1 = (Button)findViewById(R.id.answer1);
answer1.setOnClickListener(answerClick);
answer1.setTag(0);

answer2 = (Button)findViewById(R.id.answer2);
answer2.setOnClickListener(answerClick);
answer2.setTag(1);

//etc
于 2012-12-03T20:33:16.607 回答