public static ArrayList<Question> getAllQuestionByType(int type) { ArrayList<Question> questions = new ArrayList<>(); SQLiteDatabase sqLiteDatabase = openConnection(); String query = "SELECT Questions. *, Images.ImageData FROM Questions " + "LEFT JOIN Images ON Questions.ImageId = Images.Id WHERE Questions.Type = ?"; Cursor cursor = sqLiteDatabase.rawQuery(query, new String[] {"" + type}); cursor.moveToFirst(); int tmp = 1; while (!cursor.isAfterLast()) { Question question = new Question(); question.index = tmp; question.id = cursor.getInt(0); question.question = fixQuestion(byteArrayToString(cursor.getBlob(2))); question.answer1 = fixAnswer(cursor.getString(3)); question.answer2 = fixAnswer(cursor.getString(4)); question.answer3 = fixAnswer(cursor.getString(5)); question.answer4 = fixAnswer(cursor.getString(6)); question.correctAnswer = cursor.getInt(7); question.fixedAnswer = cursor.getInt(9); question.isSentFixedAnswer = cursor.getInt(10); question.imageData = cursor.getBlob(11); question.type = cursor.getInt(1); questions.add(question); cursor.moveToNext(); tmp++; } cursor.close(); closeConnection(); return questions; }
public static ArrayList<Question> getQuestionsByTypeAndExamId( int type, int examId, boolean isRandom, int numberOfQuestions) { ArrayList<Question> questions = new ArrayList<>(); SQLiteDatabase sqLiteDatabase = openConnection(); String query; Cursor cursor; if (!isRandom) { query = "select tmp.* from Exams, (SELECT Questions. *, Images.ImageData FROM Questions LEFT JOIN " + "Images ON Questions.ImageId = Images.Id) as tmp where " + "Exams.ExamId=? and Exams.Type=? and Exams.QuestionId=tmp.Id"; cursor = sqLiteDatabase.rawQuery(query, new String[] {"" + examId, "" + type}); } else { query = "SELECT Questions. *, Images.ImageData FROM Questions LEFT JOIN " + "Images ON Questions.ImageId = Images.Id WHERE Questions.Type = ? " + "order by Random() limit " + numberOfQuestions; cursor = sqLiteDatabase.rawQuery(query, new String[] {"" + type}); } cursor.moveToFirst(); int tmp = 1; while (!cursor.isAfterLast()) { Question question = new Question(); question.index = tmp; question.id = cursor.getInt(0); question.question = fixQuestion(byteArrayToString(cursor.getBlob(2))); question.answer1 = fixAnswer(cursor.getString(3)); question.answer2 = fixAnswer(cursor.getString(4)); question.answer3 = fixAnswer(cursor.getString(5)); question.answer4 = fixAnswer(cursor.getString(6)); question.correctAnswer = cursor.getInt(7); question.fixedAnswer = cursor.getInt(9); question.isSentFixedAnswer = cursor.getInt(10); question.imageData = cursor.getBlob(11); question.type = cursor.getInt(1); questions.add(question); cursor.moveToNext(); tmp++; } cursor.close(); closeConnection(); return questions; }
private Question GetSingleQuestion(JSONObject json) throws JSONException { Question item = new Question(); item.id = json.getInt("id"); item.question = json.getString("question"); item.control = json.getString("control"); item.sequenceNumber = json.getInt("sequenceNumber"); if (item.control.equals("DROP")) { List<PossibleAnswer> answers = GetPossibleAnswers(json.getJSONArray("possibleanswers")); item.possibleAnswers = answers; } if (item.control.equals("FILD")) { // handle EditFieldInfo class - TBD if time permits } return item; }