【问题标题】:Can't fetch data from SQLite database in AS无法从 AS 中的 SQLite 数据库中获取数据
【发布时间】:2022-01-25 09:10:23
【问题描述】:

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

DB_Helper 方法:

private void addQuestions(QuestionsDataBase Questions) {

    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);
}

公共 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 中的方法:

private void 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
    }
}

【问题讨论】:

    标签: java android sqlite


    【解决方案1】:

    我相信您想在这里为您的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)));
    

    谢谢,已编辑。现在它显示选项,但不显示问题本身。

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

    【讨论】:

    • 谢谢,已编辑。现在它显示选项,但不显示问题本身。
    • 更新了代码中的另一个问题
    猜你喜欢
    • 2018-05-28
    • 2012-01-06
    • 1970-01-01
    • 1970-01-01
    • 2019-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-04
    相关资源
    最近更新 更多