简体   繁体   中英

CountDownTimer trivia game score - Android (java)

I have a trivia game that displays 10 questions and they each have a 10 second timer. I have 2 problems that do not function correctly.

Firstly, If the timer runs out on a question, it displays the next question but the timer does not reset. The textviews stay at "Time's up!" and "Time Elapsed: 10000" instead of restarting the timer on the new question that is displayed.

Lastly, on the Results page the correct score is not displayed in the textview. The percentage textview displays correctly but the score textview displays "android.widget.TextView@416473c" or some other random memory location.

The program never crashes just functions incorrectly. Any code structure or other suggestions is much appreciated! This is my first android mobile app attempt and I am slowly and strugglingly through it. Yet enjoying it! :)

QuesteionView.java

public class QuestionView extends Activity  {

    @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);
        queries = getIntent().getParcelableArrayListExtra("queries");
        timer = (TextView)findViewById(R.id.timer);
        timeElapsedView = (TextView)findViewById(R.id.timeElapsedView);
        cdTimer = new Timer(startTime, interval);
        loadQuestion();
    }

    public void loadQuestion() {

        if(i == 9) {    
            endQuiz();  
        } else {    
            if(!timerHasStarted) {
                cdTimer.start();
                timerHasStarted = true;
            } else {
                cdTimer.cancel();
                timerHasStarted = false;
            }
            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) {
                    queries.get(i).setSelectedAnswer(0);
                    if(answer == 0) {
                        correctAnswers++;
                        nextQuestion();
                    } else {
                        wrongAnswers++;
                        nextQuestion();
                    }
                }
            });

           //same type of code for buttons for answers 2 through 4.
                } 
    }

    public void nextQuestion() {
        score = score + timeElapsed;
        i++;
        loadQuestion();
    }

    public class Timer extends CountDownTimer {     
        public Timer(long startTime, long interval) {
            super(startTime, interval);
        }

        public void onFinish() {
            if(i == 9) {
                cdTimer.cancel();
            } else {
                timer.setText("Time's up!");
                timeElapsedView.setText("Time Elapsed: " + String.valueOf(startTime));
                wrongAnswers++;
                nextQuestion();
            }
        }

        public void onTick(long millisUntilFinished) {
            timer.setText("Time remain: " + Long.toString(millisUntilFinished));
            timeElapsed = startTime - millisUntilFinished;
            timeElapsedView.setText("Time Elapsed: " + Long.toString(timeElapsed));
        }
    }

    public void endQuiz() {
        Intent intent = new Intent(QuestionView.this, Results.class);
        intent.putExtra("correctAnswers", correctAnswers);
        intent.putExtra("wrongAnswers", wrongAnswers);
        intent.putExtra("score", score);
        intent.putParcelableArrayListExtra("queries", queries);
        startActivity(intent);
    }
}

Results.java

    public class Results extends Activity {

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

    int cAnswers;
    int wAnswers;

    long score;

    ArrayList<Question> qs;

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

        cAnswers = getIntent().getIntExtra("correctAnswers", -1);
        wAnswers = getIntent().getIntExtra("wrongAnswers", -1);
        score = getIntent().getLongExtra("score", -1);

        qs = getIntent().getParcelableArrayListExtra("queries");

        Button mainmenuBtn = (Button)findViewById(R.id.mainmenuBtn);
        mainmenuBtn.setText("Main Menu");

        mainmenuBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                restart();
            }
        });

        showResults();
    }

    public void showResults() {

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

        TextView header = (TextView)findViewById(R.id.header);
        header.setText("SUMMARY");

        TextView percentage = (TextView)findViewById(R.id.percentage);
        percentage.setText(Integer.toString(10 * cAnswers) + "%");

        TextView score = (TextView)findViewById(R.id.score);
        String s = "" + score;
        score.setText(s);

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

        for(int i = 0; i < tList.size(); i++) {
            tList.get(i).setText(qs.get(i).getQuery());
            if(qs.get(i).getSelectedAnswer() == qs.get(i).getCorrectAnswer()) {
                tList.get(i).setTextColor(Color.GREEN);
            } else {
                tList.get(i).setTextColor(Color.RED);
            }
        }
    }

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

From all of that code, this is what I think is happening

Firstly, If the timer runs out on a question, it displays the next question but the timer does not reset. The textviews stay at "Time's up!" and "Time Elapsed: 10000" instead of restarting the timer on the new question that is displayed.

This appears to be due to you not setting your timerHasStarted variable to false after the time runs out so I would set that to false probably when you load your next question or after you show the results.

Lastly, on the Results page the correct score is not displayed in the textview. The percentage textview displays correctly but the score textview displays "android.widget.TextView@416473c" or some other random memory location.

This is because you are setting your q variables to the textview and getting the id. You need something like q1.getText().toString()

You have multiple variables with the same name score. So change it to

  TextView score2 = (TextView)findViewById(R.id.score);
  String s = "" + score;
  score2.setText(s);

the score displays not what you expected because you assigned the String s = "" + score where score is what you named for the Textview which obviously not an integer and not equivalent to the score that the user has. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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