Search code examples
androidrandomandroid-sqlitesqliteopenhelper

How to randomize questions without repetition from SQLite?


I'm developing a simple quiz in my android studio. I'm using a database but I would like to randomize the questions every time the user clicks on the quiz button. I have created 5 sample questions so far.


Solution

  • Use Collections.shuffle on your List<Question>, then use an Iterator<Question> to pick out the questions as you click a button.

    Assuming you implemented both equals and hashcode for your Question class, then a LinkedHashSet will remove the duplicates, so repetition won't happen.

    Here is an Activity based on your class that should show these concepts.

    public class QuizActivityMarketing extends Activity {
    
        private Iterator<Question> questionIterator;
        private TextView txtQuestion;
        private RadioButton rda, rdb, rdc, rdd;
        private Button butNext;
    
        private Question currentQ;
        private int score = 0;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // Define your views
            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);
    
            // Get and randomize the questions
            DbHelperMarketing db = new DbHelperMarketing(this);
            final List<Question> questions = db.getAllQuestions();
            Collections.shuffle(questions);
            questionIterator = new LinkedHashSet<Question>(questions).iterator();
    
            // Setup the first question
            if (questionIterator.hasNext()) {
                currentQ = questionIterator.next();
                setQuestionView(currentQ);
            }
    
            // Hook up the button-clicking
            butNext.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
    
                    // Check the correct answer
                    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);
                    }
    
                    // Load the next question, if there are any
                    if (questionIterator.hasNext()) {
                        currentQ = questionIterator.next();
                        setQuestionView(currentQ);
                    } else { // Done asking questions
                        setQuestionView(null);
    
                        Intent intent = new Intent(QuizActivityMarketing.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();
                    }
                }
            });
        }
    
        private void setQuestionView(Question currentQuestion) {
            if (currentQuestion != null) {
                txtQuestion.setText(currentQuestion.getQUESTION());
                rda.setText(currentQuestion.getOPTA());
                rdb.setText(currentQuestion.getOPTB());
                rdc.setText(currentQuestion.getOPTC());
                rdd.setText(currentQuestion.getOPTD());
            } else {
                txtQuestion.setText("");
                rda.setText("");
                rdb.setText("");
                rdc.setText("");
                rdd.setText("");
            }
        }
    
    }