Reputation: 15
I have a quiz application what show few questions from the SQLite Db, it has 50 questions, but it must be show 5 questions in the game part and will be randomize these.
I tired many times for the solution, but nothing :/
In the source code qid is the Db. ID primery key which is based on show the questions and find the answer.
Here is the Activity:
package com.example.quiz;
import java.util.List;
import java.util.Random;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.DatabaseUtils;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class QuizActivity extends Activity {
private static final DbHelper remote = null;
List <Question> quesList;
int score = 0;
Question currentQ;
TextView txtQuestion;
RadioButton rda, rdb, rdc, rdd;
Button butNext;
int qid;
int qNumber = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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);
rdd = (RadioButton)findViewById(R.id.radio3);
butNext = (Button)findViewById(R.id.button1);
setQuestionView();
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);
}
if(qNumber < 5){
currentQ = quesList.get(qid);
setQuestionView();
}
else
{
Intent intent = new Intent(QuizActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); //Pontunk
intent.putExtras(b); //Pont tovább vivése a következő kérdéshez
startActivity(intent);
finish();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.quiz, menu);
return true;
}
private void setQuestionView()
{
txtQuestion.setText(currentQ.getQUESTION());
rda.setText(currentQ.getOPTA());
rdb.setText(currentQ.getOPTB());
rdc.setText(currentQ.getOPTC());
rdd.setText(currentQ.getOPTD());
Random rand = new Random();
for(int i=0; i<5;i++){
int r1 = rand.nextInt(50) + 1;
qid = r1;
}
qNumber++;
}
}
When qNumber's value more than 5, it redirect to the ResultsActivity. I tested it, and its work, show the random questions, but sometimes the program crashed. Where I wrong it, and why dont work the randomization normally?
Thanks for the help, and sorry my english knowledge!
Upvotes: 1
Views: 556
Reputation: 27237
You can fetch the random questions by running this query:
db.rawQuery("SELECT * FROM YOUR_TABLE ORDER BY RANDOM() LIMIT 5", null);
Upvotes: 1
Reputation: 67
The most important thing to do is to check the crash information: that will tell you where to look.
However, I think I can see the problem. Your random number generator returns a value between 0 and 49 and then adds 1, placing a value in variable qid in the range 1 to 50. But a List is zero-based, so when you access element 50 it crashes. (And question 0 will never be accessed).
Upvotes: 0