atultiwari

QuestionActivity.java

Jul 16th, 2013
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.32 KB | None | 0 0
  1. // ---- QuestionActivity ---- //
  2. package com.atultiwari.medicalquiz;
  3.  
  4. import java.util.List;
  5.  
  6. import android.app.Activity;
  7. import android.content.Intent;
  8. import android.os.Bundle;
  9. import android.view.KeyEvent;
  10. import android.view.View;
  11. import android.view.View.OnClickListener;
  12. import android.widget.Button;
  13. import android.widget.RadioButton;
  14. import android.widget.TextView;
  15.  
  16. /**
  17.  * @author robert.hinds
  18.  *
  19.  */
  20. public class QuestionActivity extends Activity implements OnClickListener{
  21.  
  22.     private Question currentQ;
  23.     private GamePlay currentGame;
  24.  
  25.     //private WebView webView;
  26.    
  27.     @Override
  28.     public void onCreate(Bundle savedInstanceState) {
  29.         super.onCreate(savedInstanceState);
  30.         setContentView(R.layout.question);
  31.         /**
  32.          * Configure current game and get question
  33.          */
  34.         currentGame = ((QuizApplication)getApplication()).getCurrentGame();
  35.         currentQ = currentGame.getNextQuestion();
  36.         Button nextBtn = (Button) findViewById(R.id.nextBtn);
  37.         nextBtn.setOnClickListener(this);
  38.        
  39.         /**
  40.          * Update the question and answer options..
  41.          */
  42.         setQuestions();
  43.        
  44.     }
  45.  
  46.  
  47.     /**
  48.      * Method to set the text for the question and answers from the current games
  49.      * current question
  50.      */
  51.     private void setQuestions() {
  52.         //set the question text from current question
  53.         String question = Utility.capitalise(currentQ.getQuestion());
  54.         TextView qText = (TextView) findViewById(R.id.question);
  55.         qText.setText(question);
  56.        
  57.  
  58.         //set the available options
  59.         List<String> answers = currentQ.getQuestionOptions();
  60.         TextView option1 = (TextView) findViewById(R.id.answer1);
  61.         option1.setText(Utility.capitalise(answers.get(0)));
  62.        
  63.         TextView option2 = (TextView) findViewById(R.id.answer2);
  64.         option2.setText(Utility.capitalise(answers.get(1)));
  65.        
  66.         TextView option3 = (TextView) findViewById(R.id.answer3);
  67.         option3.setText(Utility.capitalise(answers.get(2)));
  68.        
  69.         TextView option4 = (TextView) findViewById(R.id.answer4);
  70.         option4.setText(Utility.capitalise(answers.get(3)));
  71.     }
  72.    
  73.  
  74.     @Override
  75.     public void onClick(View arg0) {
  76.         //Log.d("Questions", "Moving to next question");
  77.        
  78.         /**
  79.          * validate a checkbox has been selected
  80.          */
  81.         if (!checkAnswer()) return;
  82.  
  83.        
  84.         /**
  85.          * check if end of game
  86.          */
  87.         if (currentGame.isGameOver()){
  88.             //Log.d("Questions", "End of game! lets add up the scores..");
  89.             //Log.d("Questions", "Questions Correct: " + currentGame.getRight());
  90.             //Log.d("Questions", "Questions Wrong: " + currentGame.getWrong());
  91.             Intent i = new Intent(this, EndgameActivity.class);
  92.             startActivity(i);
  93.             finish();
  94.         }
  95.         else{
  96.             Intent i = new Intent(this, QuestionActivity.class);
  97.             startActivity(i);
  98.             finish();
  99.         }
  100.     }
  101.    
  102.    
  103.     @Override
  104.     public boolean onKeyDown(int keyCode, KeyEvent event)
  105.     {
  106.         switch (keyCode)
  107.         {
  108.         case KeyEvent.KEYCODE_BACK :
  109.             return true;
  110.         }
  111.        
  112.         return super.onKeyDown(keyCode, event);
  113.     }
  114.  
  115.  
  116.     /**
  117.      * Check if a checkbox has been selected, and if it
  118.      * has then check if its correct and update gamescore
  119.      */
  120.     private boolean checkAnswer() {
  121.         String answer = getSelectedAnswer();
  122.         if (answer==null){
  123.             //Log.d("Questions", "No Checkbox selection made - returning");
  124.             return false;
  125.         }
  126.         else {
  127.             //Log.d("Questions", "Valid Checkbox selection made - check if correct");
  128.             if (currentQ.getAnswer().equalsIgnoreCase(answer))
  129.             {
  130.                 //Log.d("Questions", "Correct Answer!");
  131.                 currentGame.incrementRightAnswers();
  132.             }
  133.             else{
  134.                 //Log.d("Questions", "Incorrect Answer!");
  135.                 currentGame.incrementWrongAnswers();
  136.             }
  137.             return true;
  138.         }
  139.     }
  140.  
  141.  
  142.     /**
  143.      *
  144.      */
  145.     private String getSelectedAnswer() {
  146.         RadioButton c1 = (RadioButton)findViewById(R.id.answer1);
  147.         RadioButton c2 = (RadioButton)findViewById(R.id.answer2);
  148.         RadioButton c3 = (RadioButton)findViewById(R.id.answer3);
  149.         RadioButton c4 = (RadioButton)findViewById(R.id.answer4);
  150.         if (c1.isChecked())
  151.         {
  152.             return c1.getText().toString();
  153.         }
  154.         if (c2.isChecked())
  155.         {
  156.             return c2.getText().toString();
  157.         }
  158.         if (c3.isChecked())
  159.         {
  160.             return c3.getText().toString();
  161.         }
  162.         if (c4.isChecked())
  163.         {
  164.             return c4.getText().toString();
  165.         }
  166.        
  167.         return null;
  168.     }
  169.    
  170. }
Advertisement
Add Comment
Please, Sign In to add comment