atultiwari

SplashActivity.java

Jul 16th, 2013
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.45 KB | None | 0 0
  1. // --- SplashActivity.java ---- //
  2. package com.atultiwari.medicalquiz;
  3.  
  4. import java.io.IOException;
  5. import java.util.List;
  6.  
  7. import android.app.Activity;
  8. import android.content.Intent;
  9. import android.content.SharedPreferences;
  10. import android.database.SQLException;
  11. import android.os.Bundle;
  12. import android.view.View;
  13. import android.view.View.OnClickListener;
  14. import android.widget.Button;
  15. //import android.provider.SyncStateContract.Constants;
  16.  
  17. public class SplashActivity extends Activity implements OnClickListener {
  18.  
  19.    
  20.     @Override
  21.     protected void onCreate(Bundle savedInstanceState) {
  22.         super.onCreate(savedInstanceState);
  23.         setContentView(R.layout.welcome);
  24.        
  25.         //////////////////////////////////////////////////////////////////////
  26.         //////// GAME MENU  /////////////////////////////////////////////////
  27.         Button playBtn = (Button) findViewById(R.id.playBtn);
  28.         playBtn.setOnClickListener(this);
  29.         Button settingsBtn = (Button) findViewById(R.id.settingsBtn);
  30.         settingsBtn.setOnClickListener(this);
  31.         Button rulesBtn = (Button) findViewById(R.id.rulesBtn);
  32.         rulesBtn.setOnClickListener(this);
  33.         Button exitBtn = (Button) findViewById(R.id.exitBtn);
  34.         exitBtn.setOnClickListener(this);
  35.     }
  36.  
  37.    
  38.     /**
  39.      * Listener for game menu
  40.      */
  41.     @Override
  42.     public void onClick(View v) {
  43.         Intent i;
  44.        
  45.         switch (v.getId()){
  46.         case R.id.playBtn :
  47.             //once logged in, load the main page
  48.             //Log.d("LOGIN", "User has started the game");
  49.            
  50.             //Get Question set //
  51.             List<Question> questions = getQuestionSetFromDb();
  52.  
  53.             //Initialise Game with retrieved question set ///
  54.             GamePlay c = new GamePlay();
  55.             c.setQuestions(questions);
  56.             c.setNumRounds(getNumQuestions());
  57.             ((QuizApplication)getApplication()).setCurrentGame(c);  
  58.            
  59.             //Start Game Now.. //
  60.             i = new Intent(this, QuestionActivity.class);
  61.         startActivityForResult(i, Constants.PLAYBUTTON);
  62.             break;
  63.            
  64.         case R.id.rulesBtn :
  65.             i = new Intent(this, RulesActivity.class);
  66.             startActivityForResult(i, Constants.RULESBUTTON);
  67.             break;
  68.            
  69.         case R.id.settingsBtn :
  70.             i = new Intent(this, SettingsActivity.class);
  71.             startActivityForResult(i, Constants.SETTINGSBUTTON);
  72.             break;
  73.            
  74.         case R.id.exitBtn :
  75.             finish();
  76.             break;
  77.         }
  78.  
  79.     }
  80.  
  81.  
  82.     /**
  83.      * Method that retrieves a random set of questions from
  84.      * the database for the given difficulty
  85.      * @return
  86.      * @throws Error
  87.      */
  88.     private List<Question> getQuestionSetFromDb() throws Error {
  89.         int diff = getDifficultySettings();
  90.         int numQuestions = getNumQuestions();
  91.         DBHelper myDbHelper = new DBHelper(this);
  92.         try {
  93.             myDbHelper.createDataBase();
  94.         } catch (IOException ioe) {
  95.             throw new Error("Unable to create database");
  96.         }
  97.         try {
  98.             myDbHelper.openDataBase();
  99.         }catch(SQLException sqle){
  100.             throw sqle;
  101.         }
  102.         List<Question> questions = myDbHelper.getQuestionSet(diff, numQuestions);
  103.         myDbHelper.close();
  104.         return questions;
  105.     }
  106.  
  107.  
  108.     /**
  109.      * Method to return the difficulty settings
  110.      * @return
  111.      */
  112.     private int getDifficultySettings() {
  113.         SharedPreferences settings = getSharedPreferences(Constants.SETTINGS, 0);
  114.         int diff = settings.getInt(Constants.DIFFICULTY, Constants.MEDIUM);
  115.         return diff;
  116.     }
  117.  
  118.     /**
  119.      * Method to return the number of questions for the game
  120.      * @return
  121.      */
  122.     private int getNumQuestions() {
  123.         SharedPreferences settings = getSharedPreferences(Constants.SETTINGS, 0);
  124.         int numRounds = settings.getInt(Constants.NUM_ROUNDS, 20);
  125.         return numRounds;
  126.     }
  127.    
  128. }
Advertisement
Add Comment
Please, Sign In to add comment