簡體   English   中英

無法從 AS 中的 SQLite 數據庫中獲取數據

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

我正在 Android Studio 中創建一個測驗應用程序,因此我創建了一個包含問題的數據庫。 在助手 class 中,我有一個方法 getAllQuestions 將問題從數據庫傳輸到數組列表,因此我可以提取 Main 中的 Questions 和 Buttons 中的 setText。 但是,當我運行我的應用程序時,按鈕和問題是空的,如果列表本身為空,應用程序不會拋出 nullpointer 之類的異常。 截圖

DB_Helper 方法:

私人無效添加問題(問題數據庫問題){

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

私人無效fecthDB(){

    DB_Helper db = new DB_Helper(this);
    questionList = db.getAllQuestions();
    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 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)));

謝謝,已編輯。 現在它顯示選項,但不顯示問題本身。

您將 text 設置為questionCount兩次,后者將覆蓋第一個。 也許第一個應該設置問題 textview 代替。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM