首页 > 解决方案 > 限制 Android 测验应用程序中的问题,我的问题列表来自 Firebase 实时数据库

问题描述

如果我在一个类别中有 100 个问题,如何限制问题、完成和禁用游戏?我只想将其限制为 20 个问题,然后转到结果并禁用开始游戏按钮。

我的 100 个问题存储在 Java Common.class ".List<>" 中。

我有一个包含问题和相应答案的“问题”类。

普通类:

public class Common {
    public static List<Question> questionList = new ArrayList<>();
}

显示问题方法:

private void showQuestion(final int index) {
    if (index < totalQuestion) {
        thisQuestion++;
        txtQuestionNum.setText(String.format("%d / %d", thisQuestion, totalQuestion));
        progressBar.setProgress(100);
        progressValue = 0;
        question_text.setText(Common.questionList.get(index).getQuestion());

        //if question is text we will set image to invisible
        btnA.setText(Common.questionList.get(index).getAnswerA());
        btnB.setText(Common.questionList.get(index).getAnswerB());
        btnC.setText(Common.questionList.get(index).getAnswerC());
        btnD.setText(Common.questionList.get(index).getAnswerD());

        mCountDown.start();
    }
    else {
        // if it is final question
        Intent intent = new Intent(Playing.this, Done.class);
        Bundle dataSend = new Bundle();
        dataSend.putInt("SCORE", score);
        dataSend.putInt("TOTAL", totalQuestion);
        dataSend.putInt("CORRECT", correctAnswer);
        intent.putExtras(dataSend);
        startActivity(intent);
        finish();
    }
}

问题.类:

public class Question {
    private String Question,AnswerA,AnswerB,AnswerC,AnswerD,CorrectAnswer,CategoryId;

    public Question() {
    }

    public Question(String question, String answerA, String answerB, String answerC, String answerD, String correctAnswer, String categoryId) {
        Question = question;
        AnswerA = answerA;
        AnswerB = answerB;
        AnswerC = answerC;
        AnswerD = answerD;
        CorrectAnswer = correctAnswer;
        this.CategoryId = categoryId;

    }

    public String getQuestion() {
        return Question;
    }

    public void setQuestion(String question) {
        Question = question;
    }

    public String getAnswerA() {
        return AnswerA;
    }

    public void setAnswerA(String answerA) {
        AnswerA = answerA;
    }

    public String getAnswerB() {
        return AnswerB;
    }

    public void setAnswerB(String answerB) {
        AnswerB = answerB;
    }

    public String getAnswerC() {
        return AnswerC;
    }

    public void setAnswerC(String answerC) {
        AnswerC = answerC;
    }

    public String getAnswerD() {
        return AnswerD;
    }

    public void setAnswerD(String answerD) {
        AnswerD = answerD;
    }

    public String getCorrectAnswer() {
        return CorrectAnswer;
    }

    public void setCorrectAnswer(String correctAnswer) {
        CorrectAnswer = correctAnswer;
    }

    public String getCategoryId() {
        return CategoryId;
    }

    public void setCategoryId(String categoryId) {
        this.CategoryId = categoryId;
    }
}

标签: javaandroidfirebasefirebase-realtime-database

解决方案


当列表中有 20 个问题答案时,关闭与 firebase 的数据检索连接,然后相应地更新 UI。

像这样的东西。

if(listWithQuestionsAndAnswers.size > 20)
{
 // stop thats enough, Update UI , disable game.
} else {
  // carry on.
}

此检查应采用从 firebase 为您提供数据快照的方法。


推荐阅读