无法从 AS 中的 SQLite 数据库中获取数据

Can't fetch data from SQLite database in AS

我正在 Android Studio 中创建一个测验应用程序,因此我创建了一个包含问题的数据库。在助手 class 中,我有一个方法 getAllQuestions 将问题从数据库传输到数组列表,这样我就可以在 Main 中提取问题,在 Buttons 中提取 setText。但是,当我 运行 我的应用程序时,按钮和问题是空的,如果列表本身为空,应用程序不会抛出空指针等异常。

DB_Helper 方法:

private void addQuestions(问题数据库问题) {

    ContentValues cv =  new ContentValues();
    cv.put(QuestionTable.Column_Question,Questions.getQuestion());
    cv.put(QuestionTable.Column_Option1,Questions.getOption1());
    cv.put(QuestionTable.Column_Option2,Questions.getOption2());
    cv.put(QuestionTable.Column_Option3,Questions.getOption3());
    cv.put(QuestionTable.Column_Option4,Questions.getOption4());
    cv.put(QuestionTable.Column_Correct_Ans,Questions.getQuestion());
    db.insert(QuestionTable.Table_Name,null,cv);
}

public ArrayList getAllQuestions(){

    ArrayList<QuestionsDataBase> questionsList = new ArrayList<>();
    db = getReadableDatabase();
    String[] Projection ={
            QuestionTable._ID,
            QuestionTable.Column_Question,
            QuestionTable.Column_Option1,
            QuestionTable.Column_Option2,
            QuestionTable.Column_Option3,
            QuestionTable.Column_Option4,
            QuestionTable.Column_Correct_Ans
    };
    Cursor c = db.query(QuestionTable.Table_Name,
            Projection,
            null,
            null,
            null,
            null,
            null);
    if(c.moveToFirst()){
        do{
            QuestionsDataBase questions = new QuestionsDataBase();
            questions.setQuestion(c.getString(c.getColumnIndexOrThrow(QuestionTable.Column_Question)));
            questions.setOption1(c.getString(c.getColumnIndexOrThrow(QuestionTable.Column_Option1)));
            questions.setOption2(c.getString(c.getColumnIndexOrThrow(QuestionTable.Column_Option2)));
            questions.setOption3(c.getString(c.getColumnIndexOrThrow(QuestionTable.Column_Option3)));
            questions.setOption4(c.getString(c.getColumnIndexOrThrow(QuestionTable.Column_Option4)));
            questions.setCorrectAns(c.getInt(c.getColumnIndexOrThrow(QuestionTable.Column_Correct_Ans)));
            questionsList.add(questions);
            questionsList.add(questions);

        }while(c.moveToNext());
    }
    c.close();
    return questionsList;
}

MainActivity 中的方法:

私有无效 fecthDB(){

    DB_Helper db = new DB_Helper(this);
    questionList = db.getAllQuestions();
    startQuiz();
}

private void startQuiz() {

    questionTotalCount = questionList.size(); // the total amount of questions in the current quizactivity( is set to 10)
    Collections.shuffle(questionList); // shuffle the questions form the database that are stored in QuestionList
    showQuestions();

    nextbutton.setOnClickListener(view -> {
        if(!answered){
            if(option1.isChecked() || option2.isChecked() || option3.isChecked() || option4.isChecked())
                QuizOperations();
        }
    });

}

private void showQuestions() // 显示数据库中的问题和选项 {

    rbGroup.clearCheck();
    if(questionCounter< questionTotalCount) // if not all the questions yet answered set text to new questions
    {
        currentQuestions = questionList.get(questionCounter);
        questionCount.setText(currentQuestions.getQuestion());
        option1.setText(currentQuestions.getOption1());
        option2.setText(currentQuestions.getOption2());
        option3.setText(currentQuestions.getOption3());
        option4.setText(currentQuestions.getOption4());

        questionCounter ++; // +1 to question counter
        answered = false; // tye quiz is not yet completely answered while the current question is smaller then total
        nextbutton.setText("Next");

        questionCount.setText("Questions: " + questionCounter + "/" + questionTotalCount); // how many questions answered out of 10
    }
    else{// if all the questions are answered
        handler.postDelayed(() -> {
            Intent intent = new Intent(getApplicationContext(),QuizActivity.class); // open the next activity

        }, 500); // wait for a half second
    }
}

我相信您想在此处为 QuestionsDataBase 对象设置其他字段,而不是为数据库查询中的每一列设置 setQuestion()

            questions.setQuestion(c.getString(c.getColumnIndexOrThrow(QuestionTable.Column_Option1)));
            questions.setQuestion(c.getString(c.getColumnIndexOrThrow(QuestionTable.Column_Option2)));
            questions.setQuestion(c.getString(c.getColumnIndexOrThrow(QuestionTable.Column_Option3)));
            questions.setQuestion(c.getString(c.getColumnIndexOrThrow(QuestionTable.Column_Option4)));
            questions.setQuestion(c.getString(c.getColumnIndexOrThrow(QuestionTable.Column_Correct_Ans)));

Thanks, edited it. Now it shows the options, but doesn't show the question itself.

您将文本设置为 questionCount 两次,后者会覆盖第一个。也许第一个应该改为设置问题文本视图。