Android Open Source - BNR-Quiz Quiz Activity






From Project

Back to project page BNR-Quiz.

License

The source code is released under:

MIT License

If you think the Android project BNR-Quiz listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.plastboks.bnr_quiz;
//from  ww  w  .  j a  v a  2  s . c  om
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class QuizActivity extends Activity {

    private Button trueButton;
    private Button falseButton;
    private TextView questionTextView;
    private TextView locTextView;

    private TrueFalse[] questionBank = new TrueFalse[] {
        new TrueFalse(R.string.question_oceans, true),
        new TrueFalse(R.string.question_mideast, false),
        new TrueFalse(R.string.question_africa, false),
        new TrueFalse(R.string.question_america, true),
        new TrueFalse(R.string.question_asia, true)
    };

    private int currentIndex = 0;
    private int score = 0;

    private void updateQuestion() {
        if (this.currentIndex < this.questionBank.length) {
            int q = questionBank[this.currentIndex].getQuestion();
            this.questionTextView.setText(q);
            this.locTextView.setText(this.getCurrentLocation());
        } else {
            this.sendToResults();
        }
    }

    private String getCurrentLocation() {
        return String.format("%d/%d", this.currentIndex + 1, this.questionBank.length);
    }

    private void sendToAnswer(boolean userAnswer) {
        boolean answerIsTrue = questionBank[currentIndex].isTrueQuestion();
        Intent i = new Intent(QuizActivity.this, QuizAnswer.class);

        if (userAnswer == answerIsTrue) {
            i.putExtra(QuizAnswer.EXTRA_ANSWER, true);
            this.score += 1;
        } else {
            i.putExtra(QuizAnswer.EXTRA_ANSWER, false);
        }
        startActivityForResult(i, 0);
        this.currentIndex += 1;
    }

    private void sendToResults() {
        Intent i = new Intent(QuizActivity.this, QuizResults.class);
        i.putExtra(QuizResults.EXTRA_SCORE, this.score);
        i.putExtra(QuizResults.EXTRA_QUIZ_LENGTH, this.questionBank.length);
        startActivityForResult(i, 0);
        // resets
        this.currentIndex = 0;
        this.score = 0;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);

        this.questionTextView = (TextView)findViewById(R.id.question_text_view);
        this.locTextView = (TextView)findViewById(R.id.location_text_view);

        this.trueButton = (Button)findViewById(R.id.true_button);
        this.trueButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendToAnswer(true);
            }
        });

        this.falseButton = (Button)findViewById(R.id.false_button);
        this.falseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendToAnswer(false);
            }
        });
        this.updateQuestion();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.quiz, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        this.updateQuestion();
    }
}




Java Source Code List

net.plastboks.bnr_quiz.ApplicationTest.java
net.plastboks.bnr_quiz.QuizActivity.java
net.plastboks.bnr_quiz.QuizAnswer.java
net.plastboks.bnr_quiz.QuizResults.java
net.plastboks.bnr_quiz.TrueFalse.java