如何从firebase实时数据库子随机获取数据到android studio应用程序?

uqjltbpv  于 2023-03-24  发布在  Android
关注(0)|答案(1)|浏览(133)

我尝试从5个不同的查尔兹随机获取数据到arrayList。我的firebase数据库看起来像这样:

"Quiz": {
    "Level_1": {
      "Theme_1": [
        null,
        {
          "answerText": "1",
          "imgToken": "img1-1",
          "option1Text": "Warshaw",
          "option2Text": "Brelin",
          "option3Text": "Paris",
          "option4Text": "Prague",
          "questionID": 1,
          "questionText": "What is the capital of Poland ?"
        },
        {
           "answerText": "3",
          "imgToken": "img1-2",
          "option1Text": "Oslo",
          "option2Text": "Helsinky",
          "option3Text": "Budapest",
          "option4Text": "Wiena",
          "questionID": 2,
          "questionText": "What is the capital of Hungary ?"
        },

第10题继续提问
我有5个级别,其中有5个主题,每个主题包含10个问题。有没有办法从每5个主题中随机获得1个问题,从1个级别到Android Studio中的ArrayList使用Java?
我试图从主题1到ArrayList的所有10个问题,然后做Collections.shuffle并从第0个位置添加问题到我在测验中使用的新ArrayList,但它太复杂了,如果有超过5个级别,或超过5个主题,它将崩溃或无法工作。
此外,这段代码看起来很糟糕,我认为可以写得更好。

databaseReference.child("Quiz").child("Level_1").addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        for (DataSnapshot questions : snapshot.child("Theme_1").getChildren()){

            String getQuestion = questions.child("questionText").getValue(String.class);
            String getOption1 = questions.child("option1Text").getValue(String.class);
            String getOption2 = questions.child("option2Text").getValue(String.class);
            String getOption3 = questions.child("option3Text").getValue(String.class);
            String getOption4 = questions.child("option4Text").getValue(String.class);
            String getAnswer = questions.child("answerText").getValue(String.class);
            String imgToken = questions.child("imgToken").getValue(String.class);

            QuestionsList questionslist = new QuestionsList(getQuestion, getOption1, getOption2, getOption3, getOption4, getAnswer, imgToken);
            theme1List.add(questionslist);
            Collections.shuffle(theme1List);
        }

        for (DataSnapshot questions : snapshot.child("Theme_2").getChildren()){
            String getQuestion = questions.child("questionText").getValue(String.class);
            String getOption1 = questions.child("option1Text").getValue(String.class);
            String getOption2 = questions.child("option2Text").getValue(String.class);
            String getOption3 = questions.child("option3Text").getValue(String.class);
            String getOption4 = questions.child("option4Text").getValue(String.class);
            String getAnswer = questions.child("answerText").getValue(String.class);
            String imgToken = questions.child("imgToken").getValue(String.class);

            QuestionsList questionslist = new QuestionsList(getQuestion, getOption1, getOption2, getOption3, getOption4, getAnswer, imgToken);
            theme2List.add(questionslist);
            Collections.shuffle(theme2List);
        }

        for (DataSnapshot questions : snapshot.child("Theme_3").getChildren()){
            String getQuestion = questions.child("questionText").getValue(String.class);
            String getOption1 = questions.child("option1Text").getValue(String.class);
            String getOption2 = questions.child("option2Text").getValue(String.class);
            String getOption3 = questions.child("option3Text").getValue(String.class);
            String getOption4 = questions.child("option4Text").getValue(String.class);
            String getAnswer = questions.child("answerText").getValue(String.class);
            String imgToken = questions.child("imgToken").getValue(String.class);

            QuestionsList questionslist = new QuestionsList(getQuestion, getOption1, getOption2, getOption3, getOption4, getAnswer, imgToken);
            theme3List.add(questionslist);
            Collections.shuffle(theme3List);
        }

        for (DataSnapshot questions : snapshot.child("Theme_4").getChildren()){
            String getQuestion = questions.child("questionText").getValue(String.class);
            String getOption1 = questions.child("option1Text").getValue(String.class);
            String getOption2 = questions.child("option2Text").getValue(String.class);
            String getOption3 = questions.child("option3Text").getValue(String.class);
            String getOption4 = questions.child("option4Text").getValue(String.class);
            String getAnswer = questions.child("answerText").getValue(String.class);
            String imgToken = questions.child("imgToken").getValue(String.class);

            QuestionsList questionslist = new QuestionsList(getQuestion, getOption1, getOption2, getOption3, getOption4, getAnswer, imgToken);
            theme4List.add(questionslist);
            Collections.shuffle(theme4List);
        }

        for (DataSnapshot questions : snapshot.child("Theme_5").getChildren()){
            String getQuestion = questions.child("questionText").getValue(String.class);
            String getOption1 = questions.child("option1Text").getValue(String.class);
            String getOption2 = questions.child("option2Text").getValue(String.class);
            String getOption3 = questions.child("option3Text").getValue(String.class);
            String getOption4 = questions.child("option4Text").getValue(String.class);
            String getAnswer = questions.child("answerText").getValue(String.class);
            String imgToken = questions.child("imgToken").getValue(String.class);

            QuestionsList questionslist = new QuestionsList(getQuestion, getOption1, getOption2, getOption3, getOption4, getAnswer, imgToken);
            theme5List.add(questionslist);
            Collections.shuffle(theme5List);
        }

        questionslists.add(0,theme1List.get(0));
        questionslists.add(1,theme2List.get(0));
        questionslists.add(2,theme3List.get(0));
        questionslists.add(3,theme4List.get(0));
        questionslists.add(4,theme5List.get(0));

        Collections.shuffle(questionslists);
        totalQuestionTV.setText("/"+questionslists.size());
        total = questionslists.size();
        startQuizTimer();

        selectQuestion(currentQuestionPosition);

    }
    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        Toast.makeText(MainActivity.this, "Failed", Toast.LENGTH_SHORT).show();
    }
});
sirbozc5

sirbozc51#

看起来你已经复制/粘贴了4次相同的代码块。在这种情况下,你通常可以使用一个循环来完成相同的结果,而不需要复制/粘贴。

databaseReference.child("Quiz").child("Level_1").addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        for (int theme=1; theme <= 5; theme++) { // 👈
            for (DataSnapshot questions : snapshot.child("Theme_"+theme).getChildren()) {
                String getQuestion = questions.child("questionText").getValue(String.class);
                String getOption1 = questions.child("option1Text").getValue(String.class);
                String getOption2 = questions.child("option2Text").getValue(String.class);
                String getOption3 = questions.child("option3Text").getValue(String.class);
                String getOption4 = questions.child("option4Text").getValue(String.class);
                String getAnswer = questions.child("answerText").getValue(String.class);
                String imgToken = questions.child("imgToken").getValue(String.class);
    
                QuestionsList questionslist = new QuestionsList(getQuestion, getOption1, getOption2, getOption3, getOption4, getAnswer, imgToken);
                theme1List.add(questionslist);
                Collections.shuffle(theme1List);
    
                questionslists.add(theme-1, theme1List.get(0)); // 👈
            }
        }

        Collections.shuffle(questionslists);
        totalQuestionTV.setText("/"+questionslists.size());
        total = questionslists.size();
        startQuizTimer();

        selectQuestion(currentQuestionPosition);
    }
    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        Toast.makeText(MainActivity.this, "Failed", Toast.LENGTH_SHORT).show();
    }
});

相关问题