vendredi 1 janvier 2016

Android - Cursor moving to next ID and saving the position

For my quiz application I'm working with the android cursor. In my case I want to get one question with all possible answers for a selected topic (a question can have up to 5 answers). To achieve this I have written following method:

public Cursor getQuestionItems(int topic) {
    SQLiteDatabase db = getReadableDatabase();
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    Cursor questionCursor = db.rawQuery("Select * FROM Question LEFT OUTER JOIN Answer WHERE Question.topic = "
            + topic + " AND Question._id = Answer.qid", null);

    return questionCursor;
}

If the user clicks on the "next Question" following method will be executed:

    public void getNewQuestion() {
    // // get a random id
    // Random random = new Random();
    // randomId = random.nextInt(totalQuestions) + 1;

    cursor = db.getQuestionItems(topic);

    // get the question from the database
    if (cursor.moveToFirst()) {
        question = new Question();
        question.setId(cursor.getString(cursor.getColumnIndexOrThrow(MyDatabase.QuestionColumns.ID)));
        question.setQuestiontext(cursor.getString(cursor.getColumnIndexOrThrow(MyDatabase.QuestionColumns.TEXT)));
        question.setQuestiontype(cursor.getInt(cursor.getColumnIndexOrThrow(MyDatabase.QuestionColumns.TYPE)));
        questionList.add(question);
    }

    // get the answers from the database
    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
        answer = new Answer();
        answer.setId(cursor.getString(cursor.getColumnIndexOrThrow(MyDatabase.AnswerColumns.ID)));
        answer.setAnswertext(cursor.getString(cursor.getColumnIndexOrThrow(MyDatabase.AnswerColumns.ANSWERTEXT)));
        answer.setCorrect(cursor.getInt(cursor.getColumnIndexOrThrow(MyDatabase.AnswerColumns.CORRECT)) > 0);
        answer.setChronology(cursor.getInt(cursor.getColumnIndexOrThrow(MyDatabase.AnswerColumns.CHRONOLOGY)));
        answer.setQid(cursor.getString(cursor.getColumnIndexOrThrow(MyDatabase.AnswerColumns.QID)));
        answerList.add(answer);
    }

    tv_question.setText(question.getQuestiontext() != null ? question.getQuestiontext() : null);

    if (!answerList.isEmpty()) {
        answer1.setText(answerList.size() >= 1 ? answerList.get(0).getAnswertext() : null);
        answer2.setText(answerList.size() >= 2 ? answerList.get(1).getAnswertext() : null);
        answer3.setText(answerList.size() >= 3 ? answerList.get(2).getAnswertext() : null);
        answer4.setText(answerList.size() >= 4 ? answerList.get(3).getAnswertext() : null);
        answer5.setText(answerList.size() >= 5 ? answerList.get(4).getAnswertext() : null);
    }

}

The problem is that I'm getting ALL questions and ALL answers for the selected topic (for example topic 2). But my goal is getting the question with the answers for only one id for a better performance.

So I thought about an option to move the cursor to the next id if the user clicks on "next Question" (with saving the current position of the cursor).

Is this the right way to achieve this or am I completely wrong?

Aucun commentaire:

Enregistrer un commentaire