My problem is my switch statement is not working correctly as it is not allowing for me to retrieve the data from my sqlite database correctly using a bundle extra.
My aim is if you click the 'Topic1' or 'Topic2' buttons it puts a relevant value into the bundle extra in the tutorialpage.java and using that value in the quizactivity.java it loads questions with the specific value of qid's from the sqlite database, so topic1 will have the questions with qid of 1-3 and topic2 will have the qid of 4-5.
Currently without the switch statement, using only the code in case 1 on its own, this program will retrieve the questions up to what ever value is given, however in this program it doesn't do this properly, it currently only shows question 1 on both quizzes. The next question button does not work, it acts as if there is no onclick method on it when it should go to the next question in the selected range.
As you can see from some commented out sections I get nullpointerexceptions as well.
My files are as follows:
tutorialpage.java, the tutorial page xml has two buttons with id's of Topic1 and Topic2, this is a switch statement to put an intent extra for which button is clicked.
public void GotoTheQuizButton (View view) {
Intent intentStart = new Intent(tutorialpage.this, QuizActivity.class);
Bundle bundle = new Bundle();
switch(view.getId()) {
case R.id.chapter1:
/* intentStart.putExtra(Quiz_NUMBER, 1);*/
bundle.putInt(Quiz_NUMBER, 1);
intentStart.putExtras(bundle);
break;
case R.id.chapter2:
/* intentStart.putExtra(Quiz_NUMBER, 2);*/
bundle.putInt(Quiz_NUMBER, 2);
intentStart.putExtras(bundle);
break;
}
startActivity(intentStart);
}
}
QuizActivity.java, this is the file where the Switch statement is not retrieving the intent extra properly so it only retrieves the first question for some reason.
public class QuizActivity extends Activity {
List<Question> quesList;
int score=0;
int qid=0;
Question currentQ;
TextView txtQuestion;
RadioButton rda, rdb, rdc;
Button butNext;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_quiz, menu);
return true;
}
private void setQuestionView()
{
txtQuestion.setText(currentQ.getQUESTION());
rda.setText(currentQ.getAnsA());
rdb.setText(currentQ.getAnsB());
rdc.setText(currentQ.getAnsC());
qid++;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tutorial_quiz);
DbHelper db=new DbHelper(this);
quesList=db.getAllQuestions();
currentQ=quesList.get(qid);
txtQuestion=(TextView)findViewById(R.id.textView1);
rda=(RadioButton)findViewById(R.id.radio0);
rdb=(RadioButton)findViewById(R.id.radio1);
rdc=(RadioButton)findViewById(R.id.radio2);
butNext=(Button)findViewById(R.id.button1);
setQuestionView();
Intent intent = getIntent();
Bundle extras = intent.getExtras();
int Quiz = intent.getIntExtra(tutorialpage.Quiz_NUMBER, 0);
/*
null pointer exception
int Quiz = intent.getStringExtra(tutorialpage.Quiz_NUMBER, 0);
int Quiz = extras.getInt(tutorialpage.Quiz_NUMBER);*/
/* Check the value being passed to the switch
Toast login = Toast.makeText(QuizActivity.this, "Welcome " + Quiz, Toast.LENGTH_LONG);
login.show();*/
switch(Quiz ) {
case 1:
Toast login = Toast.makeText(QuizActivity.this, "Welcome " + Quiz, Toast.LENGTH_LONG);
login.show();
butNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioGroup grp=(RadioGroup)findViewById(R.id.radioGroup1);
RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());
Log.d("yourans", currentQ.getANSWER() + " " + answer.getText());
if(currentQ.getANSWER().equals(answer.getText()))
{ score++; Log.d("score", "Your score" + score); }
while (qid < 3){ currentQ=quesList.get(qid);
setQuestionView();
Intent intent = new Intent(QuizActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
}
finish();
}
});
break;
case 2:
butNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioGroup grp = (RadioGroup) findViewById(R.id.radioGroup1);
RadioButton answer = (RadioButton) findViewById(grp.getCheckedRadioButtonId());
Log.d("yourans", currentQ.getANSWER() + " " + answer.getText());
if (currentQ.getANSWER().equals(answer.getText())) {
score++;
Log.d("score", "Your score" + score);
}
while (qid >= 4 && qid <= 5) {
currentQ = quesList.get(qid);
setQuestionView();
Intent intent = new Intent(QuizActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
}
finish();
}
});
break;
}
}
}
DBHelper.java this is where my questions get stored and I have labelled the questions with the qid q1-q5.
public class DbHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "triviaQuiz";
private static final String TABLE_QUEST = "quest";
private static final String KEY_ID = "id";
private static final String KEY_QUES = "question";
private static final String KEY_ANSWER = "answer"; //correct option
private static final String KEY_OPTA= "opta"; //option a
private static final String KEY_OPTB= "optb"; //option b
private static final String KEY_OPTC= "optc"; //option c
private SQLiteDatabase dbase;
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
dbase=db;
String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_QUEST + " ( "
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUES
+ " TEXT, " + KEY_ANSWER+ " TEXT, "+KEY_OPTA +" TEXT, "
+KEY_OPTB +" TEXT, "+KEY_OPTC+" TEXT)";
db.execSQL(sql);
addQuestions();
//db.close();
}
private void addQuestions()
{
Question q1 = new Question("Q1?", "Ans A", "" +
"Ans B", "Ans C", "Ans A");
this.addQuestion(q1);
Question q2 = new Question("Q2?", "Ans A", "" +
"Ans B", "Ans C", "Ans C");
this.addQuestion(q2);
Question q3 = new Question("Q3?", "Ans A", "" +
"Ans B", "Ans C", "Ans B");
this.addQuestion(q3);
Question q4 = new Question("What does q4 do?", "Ans A", "" +
"Ans B", "Ans C", "Ans C");
this.addQuestion(q4);
Question q5 = new Question("What does q5 do?", "Ans A", "" +
"Ans B", "Ans C", "Ans B");
this.addQuestion(q5);
}
// Adding new question
public void addQuestion(Question quest) {
//SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_QUES, quest.getQUESTION());
values.put(KEY_ANSWER, quest.getANSWER());
values.put(KEY_OPTA, quest.getAnsA());
values.put(KEY_OPTB, quest.getAnsB());
values.put(KEY_OPTC, quest.getAnsC());
// Inserting Row
dbase.insert(TABLE_QUEST, null, values);
}
public List<Question> getAllQuestions() {
List<Question> quesList = new ArrayList<Question>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_QUEST;
dbase=this.getReadableDatabase();
Cursor cursor = dbase.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Question quest = new Question();
quest.setID(cursor.getInt(0));
quest.setQUESTION(cursor.getString(1));
quest.setANSWER(cursor.getString(2));
quest.setAnsA(cursor.getString(3));
quest.setAnsB(cursor.getString(4));
quest.setAnsC(cursor.getString(5));
quesList.add(quest);
} while (cursor.moveToNext());
}
// return quest list
return quesList;
}
public int rowcount()
{
int row=0;
String selectQuery = "SELECT * FROM " + TABLE_QUEST;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
row=cursor.getCount();
return row;
}
}
I have a Question.java file that contains the basic getters and setters for the questions, ids, answers etc.
Aucun commentaire:
Enregistrer un commentaire