package com.example.myfirstllapp; //import android.R; import java.io.IOException; import java.util.HashMap; import java.util.List; import com.example.myfirstllapp.quiz.ChuckApplication; import com.example.myfirstllapp.quiz.Constants; import com.example.myfirstllapp.quiz.DBHelper; import com.example.myfirstllapp.quiz.GamePlay; import com.example.myfirstllapp.quiz.Question; import com.example.myfirstllapp.quiz.QuestionActivity; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.database.SQLException; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; public class Activia extends Activity implements OnClickListener { private Button mButton; private ImageView mImage; private EditText mEditText; private Button mChangeScreenButton; private Button mWebButton; private Button mPlayQuizButton; private HashMap mCharMap; @Override public void onCreate(Bundle bundle){ super.onCreate(bundle); initializeLayout(); //hashmap can be used instead of if thens populateHashMap(); // Using HashMap might not need this yet //mButton.setOnClickListener(this); } private void populateHashMap(){ mCharMap = new HashMap(); mCharMap.put("bugsbunny", R.drawable.bugs); mCharMap.put("clippy", R.drawable.clippy); mCharMap.put("duder",R.drawable.duder); mCharMap.put("cheesemouse",R.drawable.chuckecheese); } private void initializeLayout() { setContentView(R.layout.ugly_layout); mPlayQuizButton = (Button)findViewById(R.id.play_the_quiz); mPlayQuizButton.setOnClickListener(this); mButton = (Button)findViewById(R.id.button); mImage = (ImageView)findViewById(R.id.image_view); mEditText = (EditText)findViewById(R.id.edit_text); mChangeScreenButton = (Button)findViewById(R.id.switch_screen_button); mWebButton = (Button)findViewById(R.id.web_button); //using hashmap call down here, I dont know why tho mButton.setOnClickListener(this); mChangeScreenButton.setOnClickListener(this); mWebButton.setOnClickListener(this); } @Override public void onClick(View v) { switch(v.getId()){ case R.id.button : String enteredValue = mEditText.getText().toString(); enteredValue = enteredValue.toLowerCase().replace(" ", ""); if(enteredValue.length() == 0){ Toast.makeText(this," You need to enter something",Toast.LENGTH_SHORT).show(); } Integer drawableId = mCharMap.get(enteredValue); if(drawableId != null) { mImage.setImageDrawable(getResources().getDrawable(drawableId)); } else{ Toast.makeText(this,"Sorry The character you entered is not supported.",Toast.LENGTH_SHORT).show(); } mEditText.setText(""); break; case R.id.switch_screen_button : Toast.makeText(this,"it clicks",Toast.LENGTH_SHORT).show(); Intent intent = new Intent(this, CoolerActivity.class); startActivity(intent); break; case R.id.web_button : Toast.makeText(this,"it clicks",Toast.LENGTH_SHORT).show(); Intent intent2 = new Intent(this, WebActivity.class); startActivity(intent2); break; case R.id.play_the_quiz : Toast.makeText(this,"It Clicks2",Toast.LENGTH_SHORT).show(); //Get Question set // List questions = getQuestionSetFromDb(); //Initialise Game with retrieved question set /// GamePlay c = new GamePlay(); c.setQuestions(questions); c.setNumRounds(getNumQuestions()); ((ChuckApplication)getApplication()).setCurrentGame(c); //Start Game Now.. // Intent i = new Intent(this, QuestionActivity.class); startActivityForResult(i, Constants.PLAYBUTTON); break; } } /** * Method that retrieves a random set of questions from * the database for the given difficulty * @return * @throws Error */ private List getQuestionSetFromDb() throws Error { int diff = getDifficultySettings(); int numQuestions = getNumQuestions(); DBHelper myDbHelper = new DBHelper(this); try { myDbHelper.createDataBase(); } catch (IOException ioe) { throw new Error("Unable to create database"); } try { myDbHelper.openDataBase(); }catch(SQLException sqle){ throw sqle; } List questions = myDbHelper.getQuestionSet(diff, numQuestions); myDbHelper.close(); return questions; } /** * Method to return the difficulty settings * @return */ private int getDifficultySettings() { SharedPreferences settings = getSharedPreferences(Constants.SETTINGS, 0); int diff = settings.getInt(Constants.DIFFICULTY, Constants.MEDIUM); return diff; } /** * Method to return the number of questions for the game * @return */ private int getNumQuestions() { SharedPreferences settings = getSharedPreferences(Constants.SETTINGS, 0); int numRounds = settings.getInt(Constants.NUM_ROUNDS, 20); return numRounds; }