Advertisement
Guest User

QuestionActivity

a guest
May 25th, 2015
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.26 KB | None | 0 0
  1. package tvz.oop2.java.quizit;
  2.  
  3. import java.io.IOException;
  4. import java.util.List;
  5.  
  6.  
  7.  
  8. import tvz.oop2.java.quizit.database.DatabaseHelper;
  9. import tvz.oop2.java.quizit.helper.Game;
  10. import tvz.oop2.java.quizit.helper.Question;
  11. import tvz.oop2.java.quizit.helper.QuizApp;
  12. import android.support.v7.app.ActionBarActivity;
  13. import android.app.Activity;
  14. import android.content.Intent;
  15. import android.database.SQLException;
  16. import android.os.Bundle;
  17. import android.util.Log;
  18. import android.view.KeyEvent;
  19. import android.view.Menu;
  20. import android.view.MenuItem;
  21. import android.view.View;
  22. import android.view.View.OnClickListener;
  23. import android.widget.Button;
  24. import android.widget.RadioButton;
  25. import android.widget.TextView;
  26.  
  27. public class QuestionActivity extends Activity implements OnClickListener{
  28.  
  29.     private Question currentQ = new Question();
  30.     private Game currentGame = new Game();
  31.     private int i;
  32.     @Override
  33.     protected void onCreate(Bundle savedInstanceState) {
  34.         super.onCreate(savedInstanceState);
  35.         setContentView(R.layout.activity_question);
  36.         int numOfR;
  37.         int round;
  38.        
  39.         Bundle extras = getIntent().getExtras();
  40.         int topic = extras.getInt("topicId");
  41.  
  42.         List<Question> questions;
  43.        
  44.         questions = getQuestionsFromDb(topic);
  45.        
  46.         numOfR = questions.size();
  47.        
  48.         currentGame.setRight(0);
  49.         currentGame.setWrong(0);
  50.         currentGame.setToppic(topic);
  51.         currentGame.setNumRounds(numOfR);
  52.        
  53. //        round = 1;
  54.       // for(int i = round; i <= numOfR; i++){
  55.            
  56.         currentGame.setRound(1);
  57.        
  58.         for(i = currentGame.getRound(); i < numOfR;)
  59.         {
  60.             currentQ.setQuestion(questions.get(i).getQuestion());
  61.             currentQ.setAnswer(questions.get(i).getAnswer());
  62.             currentQ.setOption1(questions.get(i).getOption1());
  63.             currentQ.setOption2(questions.get(i).getOption2());
  64.             currentQ.setOption3(questions.get(i).getOption3());
  65.            
  66.             setQuestions();
  67.            
  68.             Log.d("drek", "Stari i " + i);
  69.  
  70.             Button  nextBtn = (Button) findViewById(R.id.nextBtn);
  71.             nextBtn.setOnClickListener(this);
  72.            
  73.             // nakon ovog bi se Round od currentGamea trebal povećat za jedan i trebala bi se postavit
  74.             // nova pitanja i odgovori. kak da to napravim?
  75.             i = currentGame.getRound();
  76.             Log.d("drek", "Novi i " + i);
  77.  
  78.        };
  79.     }
  80.        
  81.    
  82.        
  83. //        Game g = new Game();
  84. //      g.setQuestions(questions);
  85. //      int numOfRounds = questions.size();
  86. //      g.setNumRounds(numOfRounds);
  87.        
  88. //       currentGame = ((QuizApp)getApplication()).getCurrentGame();
  89. //       currentQ = currentGame.getNextQuestion();
  90.          
  91.     //}
  92.    
  93.     private List<Question> getQuestionsFromDb(int t) throws Error {
  94.         DatabaseHelper myDbHelper = new DatabaseHelper(this);
  95.         int topic = t;
  96.         int numOfQ;
  97.         try{
  98.             myDbHelper.createDataBase();
  99.         } catch (IOException ioe){
  100.             throw new Error("Unable to create database");
  101.         }
  102.        
  103.         try{
  104.             myDbHelper.openDataBase();
  105.         }catch(SQLException sqle){
  106.             throw sqle;
  107.         }
  108.        
  109.         numOfQ = myDbHelper.getNumberOfQuestions(topic);
  110.         List<Question> questions = myDbHelper.getQuestionSet(topic, numOfQ);
  111.         myDbHelper.close();
  112.         return questions;
  113.     }
  114.    
  115.     private void setQuestions(){
  116.        
  117.         String question = currentQ.getQuestion();
  118.         TextView textQ = (TextView)findViewById(R.id.question);
  119.         textQ.setText(question);
  120.        
  121.         List<String> answers = currentQ.getQuestionOptions();
  122.         TextView option1 = (TextView) findViewById(R.id.answer1);
  123.         option1.setText(answers.get(0));
  124.        
  125.         TextView option2 = (TextView) findViewById(R.id.answer2);
  126.         option2.setText(answers.get(1));
  127.        
  128.         TextView option3 = (TextView) findViewById(R.id.answer3);
  129.         option3.setText(answers.get(2));
  130.        
  131.         TextView option4 = (TextView) findViewById(R.id.answer4);
  132.         option4.setText(answers.get(3));
  133.     }
  134.  
  135.    
  136.     private String getSelectedAnswer(){
  137.        
  138.         RadioButton c1 = (RadioButton)findViewById(R.id.answer1);
  139.         RadioButton c2 = (RadioButton)findViewById(R.id.answer2);
  140.         RadioButton c3 = (RadioButton)findViewById(R.id.answer3);
  141.         RadioButton c4 = (RadioButton)findViewById(R.id.answer4);
  142.         if (c1.isChecked())
  143.         {
  144.             return c1.getText().toString();
  145.         }
  146.         if (c2.isChecked())
  147.         {
  148.             return c2.getText().toString();
  149.         }
  150.         if (c3.isChecked())
  151.         {
  152.             return c3.getText().toString();
  153.         }
  154.         if (c4.isChecked())
  155.         {
  156.             return c4.getText().toString();
  157.         }
  158.        
  159.         return null;
  160.     }
  161.    
  162.     private boolean checkAnswer(){
  163.        
  164.         String answer = getSelectedAnswer();
  165.         if (answer==null)
  166.         {
  167.             return false;
  168.         }
  169.         else
  170.         {
  171.             if (currentQ.getAnswer().equalsIgnoreCase(answer))
  172.             {
  173.                 Log.d("drek", "Stari točni score " + currentGame.getRight());
  174.                 currentGame.incrementRightAnswers();
  175.                 currentGame.setRound(currentGame.getRound() + 1);
  176.                 Log.d("drek", "Novi točni score " + currentGame.getRight());
  177.  
  178.                
  179.                 if (currentGame.isGameOver()){
  180.                    
  181.                     Intent i = new Intent(this, ScoreActivity.class);
  182.                     startActivity(i);
  183.                     finish();
  184.                 }
  185.             }
  186.             else
  187.             {
  188.                 Intent i = new Intent(this, ScoreActivity.class);
  189.                 startActivity(i);
  190.                 finish();
  191.             }
  192.             return true;
  193.         }
  194.     }
  195.    
  196.     public boolean onKeyDown(int keyCode, KeyEvent event){
  197.         switch (keyCode)
  198.         {
  199.             case KeyEvent.KEYCODE_BACK :
  200.                 return true;
  201.         }
  202.        
  203.         return super.onKeyDown(keyCode, event);
  204.     }
  205.    
  206.    
  207.     @Override
  208.     public boolean onCreateOptionsMenu(Menu menu) {
  209.         // Inflate the menu; this adds items to the action bar if it is present.
  210.         getMenuInflater().inflate(R.menu.question, menu);
  211.         return true;
  212.     }
  213.  
  214.     @Override
  215.     public boolean onOptionsItemSelected(MenuItem item) {
  216.         // Handle action bar item clicks here. The action bar will
  217.         // automatically handle clicks on the Home/Up button, so long
  218.         // as you specify a parent activity in AndroidManifest.xml.
  219.         int id = item.getItemId();
  220.         if (id == R.id.action_settings) {
  221.             return true;
  222.         }
  223.         return super.onOptionsItemSelected(item);
  224.     }
  225.  
  226.     @Override
  227.     public void onClick(View v) {
  228.         // TODO Auto-generated method stub
  229.        
  230.        
  231.         //Provjera dali je checkbox selektiran
  232.         if (!checkAnswer()) return;
  233.         else{
  234.             checkAnswer();
  235.         }
  236.        
  237.        
  238.     }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement