Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tvz.oop2.java.quizit;
- import java.io.IOException;
- import java.util.List;
- import tvz.oop2.java.quizit.database.DatabaseHelper;
- import tvz.oop2.java.quizit.helper.Game;
- import tvz.oop2.java.quizit.helper.Question;
- import tvz.oop2.java.quizit.helper.QuizApp;
- import android.support.v7.app.ActionBarActivity;
- import android.app.Activity;
- import android.content.Intent;
- import android.database.SQLException;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.KeyEvent;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.RadioButton;
- import android.widget.TextView;
- public class QuestionActivity extends Activity implements OnClickListener{
- private Question currentQ = new Question();
- private Game currentGame = new Game();
- private int i;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_question);
- int numOfR;
- int round;
- Bundle extras = getIntent().getExtras();
- int topic = extras.getInt("topicId");
- List<Question> questions;
- questions = getQuestionsFromDb(topic);
- numOfR = questions.size();
- currentGame.setRight(0);
- currentGame.setWrong(0);
- currentGame.setToppic(topic);
- currentGame.setNumRounds(numOfR);
- // round = 1;
- // for(int i = round; i <= numOfR; i++){
- currentGame.setRound(1);
- for(i = currentGame.getRound(); i < numOfR;)
- {
- currentQ.setQuestion(questions.get(i).getQuestion());
- currentQ.setAnswer(questions.get(i).getAnswer());
- currentQ.setOption1(questions.get(i).getOption1());
- currentQ.setOption2(questions.get(i).getOption2());
- currentQ.setOption3(questions.get(i).getOption3());
- setQuestions();
- Log.d("drek", "Stari i " + i);
- Button nextBtn = (Button) findViewById(R.id.nextBtn);
- nextBtn.setOnClickListener(this);
- // nakon ovog bi se Round od currentGamea trebal povećat za jedan i trebala bi se postavit
- // nova pitanja i odgovori. kak da to napravim?
- i = currentGame.getRound();
- Log.d("drek", "Novi i " + i);
- };
- }
- // Game g = new Game();
- // g.setQuestions(questions);
- // int numOfRounds = questions.size();
- // g.setNumRounds(numOfRounds);
- // currentGame = ((QuizApp)getApplication()).getCurrentGame();
- // currentQ = currentGame.getNextQuestion();
- //}
- private List<Question> getQuestionsFromDb(int t) throws Error {
- DatabaseHelper myDbHelper = new DatabaseHelper(this);
- int topic = t;
- int numOfQ;
- try{
- myDbHelper.createDataBase();
- } catch (IOException ioe){
- throw new Error("Unable to create database");
- }
- try{
- myDbHelper.openDataBase();
- }catch(SQLException sqle){
- throw sqle;
- }
- numOfQ = myDbHelper.getNumberOfQuestions(topic);
- List<Question> questions = myDbHelper.getQuestionSet(topic, numOfQ);
- myDbHelper.close();
- return questions;
- }
- private void setQuestions(){
- String question = currentQ.getQuestion();
- TextView textQ = (TextView)findViewById(R.id.question);
- textQ.setText(question);
- List<String> answers = currentQ.getQuestionOptions();
- TextView option1 = (TextView) findViewById(R.id.answer1);
- option1.setText(answers.get(0));
- TextView option2 = (TextView) findViewById(R.id.answer2);
- option2.setText(answers.get(1));
- TextView option3 = (TextView) findViewById(R.id.answer3);
- option3.setText(answers.get(2));
- TextView option4 = (TextView) findViewById(R.id.answer4);
- option4.setText(answers.get(3));
- }
- private String getSelectedAnswer(){
- RadioButton c1 = (RadioButton)findViewById(R.id.answer1);
- RadioButton c2 = (RadioButton)findViewById(R.id.answer2);
- RadioButton c3 = (RadioButton)findViewById(R.id.answer3);
- RadioButton c4 = (RadioButton)findViewById(R.id.answer4);
- if (c1.isChecked())
- {
- return c1.getText().toString();
- }
- if (c2.isChecked())
- {
- return c2.getText().toString();
- }
- if (c3.isChecked())
- {
- return c3.getText().toString();
- }
- if (c4.isChecked())
- {
- return c4.getText().toString();
- }
- return null;
- }
- private boolean checkAnswer(){
- String answer = getSelectedAnswer();
- if (answer==null)
- {
- return false;
- }
- else
- {
- if (currentQ.getAnswer().equalsIgnoreCase(answer))
- {
- Log.d("drek", "Stari točni score " + currentGame.getRight());
- currentGame.incrementRightAnswers();
- currentGame.setRound(currentGame.getRound() + 1);
- Log.d("drek", "Novi točni score " + currentGame.getRight());
- if (currentGame.isGameOver()){
- Intent i = new Intent(this, ScoreActivity.class);
- startActivity(i);
- finish();
- }
- }
- else
- {
- Intent i = new Intent(this, ScoreActivity.class);
- startActivity(i);
- finish();
- }
- return true;
- }
- }
- public boolean onKeyDown(int keyCode, KeyEvent event){
- switch (keyCode)
- {
- case KeyEvent.KEYCODE_BACK :
- return true;
- }
- return super.onKeyDown(keyCode, event);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.question, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- //Provjera dali je checkbox selektiran
- if (!checkAnswer()) return;
- else{
- checkAnswer();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement