简体   繁体   English

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

[英]Can't fetch data from SQLite database in AS

I'm creating a Quiz app in Android Studio, and so I created a Database that contains the questions.我正在 Android Studio 中创建一个测验应用程序,因此我创建了一个包含问题的数据库。 In the helper class I have a method getAllQuestions that transfers the questions from the database to an array list, so then I can extract the Questions in Main and setText in Buttons.在助手 class 中,我有一个方法 getAllQuestions 将问题从数据库传输到数组列表,因此我可以提取 Main 中的 Questions 和 Buttons 中的 setText。 However, while I run my app the buttons and the Question are empty, without the app throwing exceptions like nullpointer if the list itself was empty.但是,当我运行我的应用程序时,按钮和问题是空的,如果列表本身为空,应用程序不会抛出 nullpointer 之类的异常。 截图

DB_Helper methods: 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);
}

public ArrayList getAllQuestions(){公共 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;
}

Methods in MainActivity: MainActivity 中的方法:

private void fecthDB(){私人无效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() // Showing the question and the options from database { 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
    }
}

I believe you want to set other fields on your QuestionsDataBase object here rather than setQuestion() for each column in the database query:我相信您想在您的QuestionsDataBase object 上设置其他字段,而不是为数据库查询中的每一列设置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.现在它显示选项,但不显示问题本身。

You are setting text to questionCount twice where the later overwrites the first one.您将 text 设置为questionCount两次,后者将覆盖第一个。 Maybe the first one should set the question textview instead.也许第一个应该设置问题 textview 代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM