Reputation: 195
I am getting confused. I am developing a quiz. I have been trying to randomize questions. I tried to randomize within the Cursor and Rawquery to no avail.
At the same time, I want the app to loop questions after the last one.
Here is my Question.java (this is my activity)
public class Question extends Activity {
public static final String TAG = Question.class.getSimpleName();
private TextView mTimer;
private Button mTimerButton;
private CountDownTimer mCountDownTimer;
private TextView mQuestionTextView;
private Button mNextQuestion;
private Button mHowtoplay2;
List<QuestionFaci> quesList;
int qid = 0;
QuestionFaci currentQ;
@Override
protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState( outState );
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState( savedInstanceState );
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_question );
mHowtoplay2 = (Button) findViewById( R.id.howtoplaybutton );
mHowtoplay2.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
startHowtoplay();
}
} );
QuizHelper db = new QuizHelper( this );
quesList = db.getAllQuestions();
currentQ = quesList.get(qid);
mQuestionTextView = (TextView) findViewById( R.id.questionView );
mNextQuestion = (Button)findViewById( R.id.nextbutton );
View.OnClickListener listener = new View.OnClickListener(){
@Override
public void onClick(View v) {
currentQ=quesList.get(qid);
setQuestionView();
}
};
mNextQuestion.setOnClickListener( listener );
Log.d(TAG, "We're logging from the onCreate() method");
private void startHowtoplay(){
Intent intent = new Intent( this, Howtoplay.class );
startActivity( intent );
}
private void setQuestionView(){
mQuestionTextView.setText( currentQ.getQUESTION() );
qid++;
}
}
Here is my QuizHelper
public class QuizHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "creativequestion";
private static final String TABLE_QUEST = "quest";
private static final String KEY_ID = "qid";
private static final String KEY_QUEST = "question";
private SQLiteDatabase dbase;
public QuizHelper(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_QUEST + " TEXT)";
db.execSQL( sql );
addQuestion();
}
private void addQuestion() {
QuestionFaci q1 = new QuestionFaci( "OTHER USES: Name other uses of a Hammer. \n\n Example: Stir a soup." );
this.addQuestion(q1);
QuestionFaci q2 = new QuestionFaci( "RHYMES: Words that rhymes with Rice. \n\n Example: Ice" );
this.addQuestion(q2);
QuestionFaci q3 = new QuestionFaci( "WITH: I can cook eggs with... \n\n Example: A Piece of Plywood" );
this.addQuestion(q3);
QuestionFaci q4 = new QuestionFaci( "WITHOUT: I can wash my clothes without... \n\n Example: My Aunt" );
this.addQuestion(q4);
QuestionFaci q5 = new QuestionFaci( "I WILL: If I was Bill Gates, I will... \n\n Example: Buy a spaceship" );
this.addQuestion(q5);
QuestionFaci q6 = new QuestionFaci( "CREATE A MOVIE TITLE: A NIGHT \n\n Example: To Remember" );
this.addQuestion(q6);
QuestionFaci q7 = new QuestionFaci( "OTHER NAMES: Other names of a cow \n\n Example: Milk giver" );
this.addQuestion(q7);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
db.execSQL( "DROP TABLE IF EXISTS" + TABLE_QUEST );
onCreate( db );
}
private void addQuestion(QuestionFaci quest) {
ContentValues values = new ContentValues( );
values.put(KEY_QUEST, quest.getQUESTION());
dbase.insert( TABLE_QUEST, null, values );
}
public List<QuestionFaci> getAllQuestions(){
List<QuestionFaci> quesList = new ArrayList<QuestionFaci>( );
String selectQuery = "SELECT * FROM " + TABLE_QUEST;
dbase = this.getReadableDatabase();
Cursor cursor = dbase.rawQuery( selectQuery, null );
if (cursor.moveToFirst()){
do{
QuestionFaci quest = new QuestionFaci( );
quest.setID( cursor.getInt( 0 ) );
quest.setQUESTION( cursor.getString( 1 ) );
quesList.add(quest);
} while (cursor.moveToNext());
}
return quesList;
}
}
Here is my QuestionFaci where my getters and setters
public class QuestionFaci extends Activity {
private int ID;
private String QUESTION;
public QuestionFaci(){
ID=0;
QUESTION= "";
}
public QuestionFaci (String qUESTION){
QUESTION = qUESTION;
}
public int getID(){
return ID;
}
public String getQUESTION(){
return QUESTION;
}
public void setID(int id){
ID = id;
}
public void setQUESTION(String qUESTION){
QUESTION = qUESTION;
}
}
Upvotes: 0
Views: 252
Reputation: 1672
Replace with this :
private void setQuestionView(){
Random r = new Random();
int Low = 0;
int High = quesList.size();
int Result = r.nextInt(High-Low) + Low; // 0 - questions size() -> select random
qid = Result;
mQuestionTextView.setText( currentQ.getQUESTION() ); // set Question
}
If you want previous questions not to appear again you have to do it in a different way.
P.S : This does not randomize your questions in your database. It randomizes the qid so it gives you a random question from your list. You could randomize the list that you get from the database, and if you use a HashMap then you will know if a question has appeared before or not.
Upvotes: 1