0

I wrote a function that fetches me a list of different random questions from a SQLite data base. In a previous stage I didn't have a check to see if an added question is really different from the other ones and I got what I expected: Sometimes the same question multiple times.

Now, with the check I get the same question every time... I feel fooled. This is the code:

public Cursor getRandomQuestion(String module) {
    int i;
    Random r = new Random();
    String sql = "SELECT ID, MODULE, QUESTION, ANSWER_1, ANSWER_2, ANSWER_3, ANSWER_4, CORRECT FROM Questions WHERE MODULE = ?";
    Cursor mCursor = mDb.rawQuery(sql,new String[]{module});
    if (mCursor != null) {
        i = mCursor.getCount();
        mCursor.moveToFirst();
        mCursor.move(r.nextInt(i-1));
    }
    return mCursor;
}

public Boolean contains(int e,int[] list){
    int i = 0;
    if(list.length == 0) return false;
    for(i=0; i<list.length;i++){
        if(list[i] == e) return true;
    }
    return false;
}

public String[][] createTable(String module){
    String[][] roundQuestions = new String[NUMBER_OF_QUESTIONS][6];
    Cursor question = null;
    int questionList[] = new int[NUMBER_OF_QUESTIONS];
    Boolean flag = false;
    //Here comes the check
    for(int i = 0; i < NUMBER_OF_QUESTIONS; i++){
        while(flag==false){
            question = getRandomQuestion(module);
            int q = question.getInt(0);
            if(contains(q,questionList)==false){
                flag = true;
                questionList[i] = q;
            }
        }
        roundQuestions[i][0] = question.getString(2);
        roundQuestions[i][1] = question.getString(3);
        roundQuestions[i][2] = question.getString(4);
        roundQuestions[i][3] = question.getString(5);
        roundQuestions[i][4] = question.getString(6);
        roundQuestions[i][5] = Integer.toString(question.getInt(7));
    }
    return roundQuestions;
}

I know it's not the most efficient of all codes, but a mere functionality suffices in this case. I already tried to use an ArrayList for questionList and its .contains(), but that resulted in the same problem.

CL.
  • 173,858
  • 17
  • 217
  • 259
ille
  • 65
  • 6

0 Answers0