Advertisement
Guest User

Untitled

a guest
Mar 30th, 2013
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.21 KB | None | 0 0
  1. package com.example.myfirstllapp;
  2.  
  3. //import android.R;
  4. import java.io.IOException;
  5. import java.util.HashMap;
  6. import java.util.List;
  7.  
  8. import com.example.myfirstllapp.quiz.ChuckApplication;
  9. import com.example.myfirstllapp.quiz.Constants;
  10. import com.example.myfirstllapp.quiz.DBHelper;
  11. import com.example.myfirstllapp.quiz.GamePlay;
  12. import com.example.myfirstllapp.quiz.Question;
  13. import com.example.myfirstllapp.quiz.QuestionActivity;
  14.  
  15. import android.app.Activity;
  16. import android.content.Intent;
  17. import android.content.SharedPreferences;
  18. import android.database.SQLException;
  19. import android.os.Bundle;
  20. import android.view.View;
  21. import android.view.View.OnClickListener;
  22. import android.widget.Button;
  23. import android.widget.EditText;
  24. import android.widget.ImageView;
  25. import android.widget.Toast;
  26.  
  27. public class Activia extends Activity implements OnClickListener {
  28.  
  29. private Button mButton;
  30. private ImageView mImage;
  31. private EditText mEditText;
  32. private Button mChangeScreenButton;
  33. private Button mWebButton;
  34. private Button mPlayQuizButton;
  35.  
  36. private HashMap<String, Integer> mCharMap;
  37.  
  38. @Override
  39. public void onCreate(Bundle bundle){
  40. super.onCreate(bundle);
  41. initializeLayout();
  42.  
  43. //hashmap can be used instead of if thens
  44. populateHashMap();
  45.  
  46.  
  47. // Using HashMap might not need this yet
  48. //mButton.setOnClickListener(this);
  49. }
  50.  
  51. private void populateHashMap(){
  52. mCharMap = new HashMap<String, Integer>();
  53. mCharMap.put("bugsbunny", R.drawable.bugs);
  54. mCharMap.put("clippy", R.drawable.clippy);
  55. mCharMap.put("duder",R.drawable.duder);
  56. mCharMap.put("cheesemouse",R.drawable.chuckecheese);
  57. }
  58.  
  59. private void initializeLayout() {
  60. setContentView(R.layout.ugly_layout);
  61. mPlayQuizButton = (Button)findViewById(R.id.play_the_quiz);
  62. mPlayQuizButton.setOnClickListener(this);
  63. mButton = (Button)findViewById(R.id.button);
  64. mImage = (ImageView)findViewById(R.id.image_view);
  65. mEditText = (EditText)findViewById(R.id.edit_text);
  66. mChangeScreenButton = (Button)findViewById(R.id.switch_screen_button);
  67. mWebButton = (Button)findViewById(R.id.web_button);
  68. //using hashmap call down here, I dont know why tho
  69. mButton.setOnClickListener(this);
  70. mChangeScreenButton.setOnClickListener(this);
  71. mWebButton.setOnClickListener(this);
  72.  
  73. }
  74.  
  75.  
  76. @Override
  77. public void onClick(View v) {
  78. switch(v.getId()){
  79. case R.id.button :
  80. String enteredValue = mEditText.getText().toString();
  81. enteredValue = enteredValue.toLowerCase().replace(" ", "");
  82.  
  83. if(enteredValue.length() == 0){
  84. Toast.makeText(this," You need to enter something",Toast.LENGTH_SHORT).show();
  85. }
  86.  
  87. Integer drawableId = mCharMap.get(enteredValue);
  88. if(drawableId != null) {
  89. mImage.setImageDrawable(getResources().getDrawable(drawableId));
  90. }
  91. else{
  92. Toast.makeText(this,"Sorry The character you entered is not supported.",Toast.LENGTH_SHORT).show();
  93. }
  94. mEditText.setText("");
  95. break;
  96. case R.id.switch_screen_button :
  97. Toast.makeText(this,"it clicks",Toast.LENGTH_SHORT).show();
  98. Intent intent = new Intent(this, CoolerActivity.class);
  99. startActivity(intent);
  100. break;
  101.  
  102. case R.id.web_button :
  103. Toast.makeText(this,"it clicks",Toast.LENGTH_SHORT).show();
  104. Intent intent2 = new Intent(this, WebActivity.class);
  105. startActivity(intent2);
  106. break;
  107.  
  108. case R.id.play_the_quiz :
  109. Toast.makeText(this,"It Clicks2",Toast.LENGTH_SHORT).show();
  110.  
  111. //Get Question set //
  112. List<Question> questions = getQuestionSetFromDb();
  113.  
  114. //Initialise Game with retrieved question set ///
  115. GamePlay c = new GamePlay();
  116. c.setQuestions(questions);
  117. c.setNumRounds(getNumQuestions());
  118. ((ChuckApplication)getApplication()).setCurrentGame(c);
  119.  
  120. //Start Game Now.. //
  121. Intent i = new Intent(this, QuestionActivity.class);
  122. startActivityForResult(i, Constants.PLAYBUTTON);
  123.  
  124.  
  125. break;
  126. }
  127. }
  128.  
  129.  
  130. /**
  131. * Method that retrieves a random set of questions from
  132. * the database for the given difficulty
  133. * @return
  134. * @throws Error
  135. */
  136. private List<Question> getQuestionSetFromDb() throws Error {
  137. int diff = getDifficultySettings();
  138. int numQuestions = getNumQuestions();
  139. DBHelper myDbHelper = new DBHelper(this);
  140. try {
  141. myDbHelper.createDataBase();
  142. } catch (IOException ioe) {
  143. throw new Error("Unable to create database");
  144. }
  145. try {
  146. myDbHelper.openDataBase();
  147. }catch(SQLException sqle){
  148. throw sqle;
  149. }
  150. List<Question> questions = myDbHelper.getQuestionSet(diff, numQuestions);
  151. myDbHelper.close();
  152. return questions;
  153. }
  154.  
  155.  
  156. /**
  157. * Method to return the difficulty settings
  158. * @return
  159. */
  160. private int getDifficultySettings() {
  161. SharedPreferences settings = getSharedPreferences(Constants.SETTINGS, 0);
  162. int diff = settings.getInt(Constants.DIFFICULTY, Constants.MEDIUM);
  163. return diff;
  164. }
  165.  
  166. /**
  167. * Method to return the number of questions for the game
  168. * @return
  169. */
  170. private int getNumQuestions() {
  171. SharedPreferences settings = getSharedPreferences(Constants.SETTINGS, 0);
  172. int numRounds = settings.getInt(Constants.NUM_ROUNDS, 20);
  173. return numRounds;
  174. }
  175.  
  176. <?xml version="1.0" encoding="utf-8"?>
  177. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  178. package="com.example.myfirstllapp"
  179. android:versionCode="1"
  180. android:versionName="1.0" >
  181.  
  182. <uses-sdk
  183. android:minSdkVersion="8"
  184. android:targetSdkVersion="9" />
  185. <uses-permission android:name="android.permission.INTERNET" />
  186.  
  187.  
  188. <application
  189. android:allowBackup="true"
  190. android:icon="@drawable/ic_launcher"
  191. android:label="@string/app_name"
  192. android:theme="@style/AppTheme"
  193.  
  194. android:debuggable="true" >
  195. <activity
  196. android:name="com.example.myfirstllapp.Activia"
  197. android:label="@string/app_name" >
  198. <intent-filter>
  199. <action android:name="android.intent.action.MAIN" />
  200. <category android:name="android.intent.category.LAUNCHER" />
  201. </intent-filter>
  202. </activity>
  203.  
  204. <activity android:name=".CoolerActivity" />
  205. <activity android:name=".WebActivity" />
  206.  
  207. <activity android:name="com.example.myfirstllapp.quiz.QuestionActivity" />
  208. <activity android:name="com.example.myfirstllapp.quiz.RulesActivity" />
  209. <activity android:name="com.example.myfirstllapp.quiz.EndgameActivity" />
  210. <activity android:name="com.example.myfirstllapp.quiz.SettingsActivity" />
  211. <activity android:name="com.example.myfirstllapp.quiz.AnswersActivity" />
  212. <activity android:name="com.example.myfirstllapp.quiz.ChuckApplication" />
  213.  
  214.  
  215. </application>
  216. <application
  217. android:name=".ChuckApplication" >
  218.  
  219.  
  220. </application>
  221.  
  222. </manifest>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement