不循环for循环 - Java(android)

时间:2012-12-03 01:19:26

标签: java android for-loop

程序正常运行并出现。当我在主菜单上单击开始时,它会显示一个问题(textview)和4个答案(按钮)。文本被分配给textview和带有for循环的4个按钮。单击按钮后,它永远不会循环。单击按钮时没有任何反应。

我尝试过“中断”并更改为while循环而无法使其正常工作。我认为这是一个我无法找到的简单修复。

赞赏并需要任何其他代码结构或建议!

public class QuestionView extends Activity {

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

    int correctAnswers = 0;
    int wrongAnswers = 0;

    int answer = 0;

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

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

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

        for(int i = 0; i < 10; i++) {
            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());

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

            answer1.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    if(answer == 0) {
                        correctAnswers++;
                    } else {
                        wrongAnswers++;
                    }
                }
            });

            answer2.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    if(answer == 1) {
                        correctAnswers++;
                    } else {
                        wrongAnswers++;
                    }
                }
            });

            answer3.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    if(answer == 2) {
                        correctAnswers++;
                    } else {
                        wrongAnswers++;
                    }
                }
            });

            answer4.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    if(answer == 3) {
                        correctAnswers++;
                    } else {
                        wrongAnswers++;
                    }
                }
            });
        }
    }
}

2 个答案:

答案 0 :(得分:1)

onCreate方法仅在创建Activity时运行。你想要做的是将以下代码分解为一个单独的方法,并从onClickLiestener()为不同的按钮调用该方法:

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

由于此解决方案不包含循环,因此您必须确保每次单击都增加索引(在本例中为“i”)。

编辑: 建议的解决方案

public class QuestionView extends Activity {

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

    int correctAnswers = 0;
    int wrongAnswers = 0;

    int answer = 0;

    int i=0;

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

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

        answer1.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                if(answer == 0) {
                    correctAnswers++;
                } else {
                    wrongAnswers++;
                }
                reloadQuestion();
            }
        });

        answer2.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                if(answer == 1) {
                    correctAnswers++;
                } else {
                    wrongAnswers++;
                }
                reloadQuestion();
            }
        });

        answer3.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                if(answer == 2) {
                    correctAnswers++;
                } else {
                    wrongAnswers++;
                }
                reloadQuestion();
            }
        });

        answer4.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                if(answer == 3) {
                    correctAnswers++;
                } else {
                    wrongAnswers++;
                }
                reloadQuestion();
            }
        });

        reloadQuestion();
    }


    private void reloadQuestion(){
        TextView question = (TextView)findViewById(R.id.question);

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

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

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

        i++;
    }
}

你可能会以不同的方式优化代码,但这个建议可以解决你的问题。

答案 1 :(得分:1)

你的程序所做的只是十次将不同的文本从“查询”ArrayList分配给一堆TextView对象。此外,它一遍又一遍地将相同的onClickListener对象分配给相同的textView对象。这一切都发生在活动创建时。因此,当您实际上能够看到您的活动时,循环结束并且所有textView项目都具有最后(第10个)值。 当您单击该按钮时,该程序将完全按照您的要求执行操作:增加其中一个计数器。

我相信这不是你的想法。你真正想做的是:

  1. 只创建一个onClickListener对象,并将onCreate()方法中的对象分配给所有textView项。
  2. 在onClickListener内部检查单击的视图(确定单击了哪个textVIew)并将其与正确答案进行比较。相应更新计数器。阅读下一组问答数据并更新用户界面。
  3. 以下是我所说的一个例子:

    public class QuestionView extends Activity {
    
        Quiz quiz = new Quiz();
        ArrayList<Question> queries = quiz.getRandom10();
    
        private int correctAnswers;
        private int wrongAnswers;
        private char mCurrentAnswer;
        private char mNextQuestion;
    
        private View.OnClickListener clickListener = new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                if (v.getId() == getCurrentAnswer()) {
                    ++correctAnswers;
                } else {
                    ++wrongAnswers;
                }
    
                readNextQuestion();
            }
        }; 
    
        private int getCurrentAnswer() {
            return mCurrentAnswer;
        }
    
        private void readNextQuestion() {
            question.setText(queries.get(mNextQuestion).getQuery());
            answer1.setText(queries.get(mNextQuestion).getA1());
            answer2.setText(queries.get(mNextQuestion).getA2());
            answer3.setText(queries.get(mNextQuestion).getA3());
            answer4.setText(queries.get(mNextQuestion).getA4());
    
            mCurrentAnswer = queries.get(mNextQuestion).getCorrectAnswer();
    
            ++mNextQuestion;
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.questionviewmain);
    
            TextView question = (TextView)findViewById(R.id.question);
    
            Button answer1 = (Button)findViewById(R.id.answer1);
            Button answer2 = (Button)findViewById(R.id.answer2);
            Button answer3 = (Button)findViewById(R.id.answer3);
            Button answer4 = (Button)findViewById(R.id.answer4);
    
            //You can also assign it in the xml (I guess)
            answer1.setId(1);
            answer1.setOnClickListener(clickListener);
            answer2.setId(2);
            answer2.setOnClickListener(clickListener);
            answer3.setId(3);
            answer3.setOnClickListener(clickListener);
            answer4.setId(4);
            answer4.setOnClickListener(clickListener);
    
            readNextQuestion();
        }
    }