Android答案列表不会移至下一个答案列表

时间:2014-08-13 15:17:56

标签: android button arraylist

我在按钮中显示4个答案时遇到了这个问题。播放类正在运行时会出现8,9,3,1,但在按下按钮后它不会移动到下一个答案列表(17,20,15,14等)...

Java

public class play extends Activity  {

private Question currentQuestion;
private int currentQuestionIndex;
private ArrayList<Button> questionButton;
private TextView questionstextview;
private TextView questionnumber;
private TextView playerfeedback;
public static TextView displayscore;
public static int score;
private List<Question> QuestionList;
private int answerchoice; 
public static int totalanswer;
public static int correctanswer;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.play);
        Log.i("playclass", "this is play class running");

        questionButton = new ArrayList<Button>();       
        questionButton.add((Button) findViewById (R.id.answerbutton1));
        questionButton.add((Button) findViewById (R.id.answerbutton2));
        questionButton.add((Button) findViewById (R.id.answerbutton3));
        questionButton.add((Button) findViewById (R.id.answerbutton4));
        currentQuestion = null;
        currentQuestionIndex = 0;

        QuestionList = new ArrayList<Question>();
        ArrayList <String> answer = new ArrayList<String>();

        answer.add("8");
        answer.add("9");
        answer.add("3");
        answer.add("1");
        QuestionList.add(new Question("what is 4+4", answer, 0));
        answer.add("17");
        answer.add("20");
        answer.add("15");
        answer.add("14");
        QuestionList.add(new Question("what is 7+8?", answer, 3));
        answer.add("20");
        answer.add("30");
        answer.add("19");
        answer.add("34");
        QuestionList.add(new Question("what is 10+10?", answer, 0));
        answer.add("12");
        answer.add("11");
        answer.add("13");
        answer.add("14");
        QuestionList.add(new Question("what is 6+6?", answer, 0));
        answer.add("6");
        answer.add("5");
        answer.add("4");
        answer.add("7");
        QuestionList.add(new Question("what is 4+3?", answer, 3));
        answer.add("7");
        answer.add("9");
        answer.add("10");
        answer.add("11");
        QuestionList.add(new Question("what is 3+7?", answer, 2));

        questionstextview = (TextView) findViewById (R.id.questionstextview);           
        questionnumber = (TextView) findViewById (R.id.questionnumber);        
        displayscore = (TextView) findViewById (R.id.displayscore);

        StartTrivia();
    }
public void ButtonPress (View answerButton){

    MediaPlayer soundfx = MediaPlayer.create(getApplicationContext(), R.raw.click);

    soundfx.start();

    for (int i=0; i< questionButton.size(); i++)
    {   


        if (questionButton.get(i) ==answerButton)

        {   

         if (i==currentQuestion.getAnswerIndex())

          { 
             score=+5;
             totalanswer++;
             correctanswer++;

            displayscore.setText(Integer.toString(score));
            Toast.makeText(getApplicationContext(), "Correct!", Toast.LENGTH_SHORT).show();

        }

        else{

            totalanswer++;

            Toast.makeText(getApplicationContext(), "Incorrect!", Toast.LENGTH_SHORT).show();
             Log.i("playclass", "after incorrect toast");
        }

            currentQuestionIndex++;


             Log.i("playclass", "after currentQuestionIndex++; ");

            if (currentQuestionIndex < QuestionList.size()){

                Log.i("playclass", "after currentQuestionIndex < QuestionList.size() ");
                StartTrivia();
                Log.i("playclass", "after StartTrivia in if statement ");
            }

            else{

                Intent result = new Intent (this, finalscreen.class);
                startActivity(result);
                Log.i("playclass", "after IntentResult ");
            }
            break;
         }

    }   
}

public void StartTrivia(){

    Log.i("playclass", "running StartTrivia() " + currentQuestionIndex); 

    currentQuestion = QuestionList.get(currentQuestionIndex); 

    questionstextview.setText(currentQuestion.getquestion());

    questionnumber.setText(Integer.toString(currentQuestionIndex+1));

    for (int i = 0; i < questionButton.size(); i++)

     { 

         String ans = currentQuestion.getanswer().get(i); 
         questionButton.get(i).setText(ans);

     }

    }

}

LogCat

08-13 14:55:02.341:I / ActivityManager(73):从pid 387开始:Intent {cmp = com.example.quizgame / .play} 08-13 14:55:02.651:I / playclass(387):这是play class running
08-13 14:55:02.671:I / playclass(387):运行StartTrivia()0
08-13 14:55:03.422:I / ActivityManager(73):显示com.example.quizgame / .play:+ 1s2ms
08-13 14:55:05.634:D / dalvikvm(387):GC_CONCURRENT释放92K,3%免费9024K / 9287K,暂停9ms + 5ms
08-13 14:55:05.711:D / AudioSink(32):bufferCount(4)太小而增加到12 08-13 14:55:05.851:I / playclass(387):在currentQuestionIndex ++之后;
08-13 14:55:05.851:I / playclass(387):在currentQuestionIndex&lt; QuestionList.size()
08-13 14:55:05.861:I / playclass(387):运行StartTrivia()1
08-13 14:55:05.861:I / playclass(387):在StartTrivia后if语句
08-13 14:55:08.712:D / AudioSink(32):bufferCount(4)太小而增加到12 08-13 14:55:08.745:I / playclass(387):不正确的吐司后 08-13 14:55:08.745:I / playclass(387):在currentQuestionIndex ++之后;
08-13 14:55:08.752:I / playclass(387):在currentQuestionIndex&lt; QuestionList.size()
08-13 14:55:08.752:I / playclass(387):运行StartTrivia()2
08-13 14:55:08.775:I / playclass(387):在ifT语句中的StartTrivia之后 08-13 14:55:09.182:I / dalvikvm(73):Jit:将JitTable从1024调整到2048左右 08-13 14:55:10.102:D / AudioSink(32):bufferCount(4)太小而增加到12 08-13 14:55:10.162:I / playclass(387):在currentQuestionIndex ++之后;
08-13 14:55:10.162:I / playclass(387):在currentQuestionIndex&lt; QuestionList.size()
08-13 14:55:10.172:I / playclass(387):运行StartTrivia()3
08-13 14:55:10.231:I / playclass(387):在if语句的StartTrivia之后
08-13 14:55:10.388:W / AudioFlinger(32):写入被阻止75毫秒,1个延迟写入,线程0xff38
08-13 14:55:11.911:D / AudioSink(32):bufferCount(4)太小而增加到12 08-13 14:55:12.011:I / playclass(387):在currentQuestionIndex ++之后;
08-13 14:55:12.011:I / playclass(387):在currentQuestionIndex&lt; QuestionList.size()
08-13 14:55:12.011:I / playclass(387):运行StartTrivia()4
08-13 14:55:12.011:I / playclass(387):在startTrivia之后的if语句中 08-13 14:55:14.011:D / AudioSink(32):bufferCount(4)太小,增加到12 08-13 14:55:14.072:I / playclass(387):不正确的吐司后 08-13 14:55:14.072:I / playclass(387):在currentQuestionIndex ++之后;
08-13 14:55:14.092:I / playclass(387):在currentQuestionIndex&lt; QuestionList.size()
08-13 14:55:14.092:I / playclass(387):运行StartTrivia()5
08-13 14:55:14.111:I / playclass(387):在if语句中的StartTrivia之后 08-13 14:55:17.691:D / AudioSink(32):bufferCount(4)太小而增加到12 08-13 14:55:17.761:I / playclass(387):不正确的吐司后 08-13 14:55:17.761:I / playclass(387):在currentQuestionIndex ++之后;
08-13 14:55:17.822:I / ActivityManager(73):从pid 387开始:Intent {cmp = com.example.quizgame / .finalscreen} 08-13 14:55:17.876:I / playclass(387):在IntentResult之后 08-13 14:55:18.112:W / AudioFlinger(32):写入被阻止76毫秒,6个延迟写入,线程0xff38
08-13 14:55:18.251:I / finalscreen(387):这是终结班级的运行
08-13 14:55:19.481:I / ActivityManager(73):显示com.example.quizgame / .finalscreen:+ 1s582ms

1 个答案:

答案 0 :(得分:4)

     ArrayList <String> answer = new ArrayList<String>();
    answer.add("8");
    answer.add("9");
    answer.add("3");
    answer.add("1");
    QuestionList.add(new Question("what is 4+4", answer, 0));

    ArrayList <String> answer1 = new ArrayList<String>();
    answer1.add("17");
    answer1.add("20");
    answer1.add("15");
    answer1.add("14");
    QuestionList.add(new Question("what is 7+8?", answer1, 3));

   ArrayList <String> answer2 = new ArrayList<String>();
    answer2.add("20");
    answer2.add("30");
    answer2.add("19");
    answer2.add("34");
    QuestionList.add(new Question("what is 10+10?", answer2, 0));

    ArrayList <String> answer3 = new ArrayList<String>();
    answer3.add("12");
    answer3.add("11");
    answer3.add("13");
    answer3.add("14");
    QuestionList.add(new Question("what is 6+6?", answer3, 0));

    ArrayList <String> answer4 = new ArrayList<String>();
    answer4.add("6");
    answer4.add("5");
    answer4.add("4");
    answer4.add("7");
    QuestionList.add(new Question("what is 4+3?", answer4, 3));

   ArrayList <String> answer5 = new ArrayList<String>();
    answer5.add("7");
    answer5.add("9");
    answer5.add("10");
    answer5.add("11");
    QuestionList.add(new Question("what is 3+7?", answer5, 2));