【问题标题】:Random questions from different child from Firebase to Java andoird studio从 Firebase 到 Java android studio 的不同孩子的随机问题
【发布时间】:2022-11-11 06:42:02
【问题描述】:

我正在尝试使用 Firebase 制作测验应用程序,但我需要从不同的主题中随机获取 5 个问题并将它们添加到 ArrayList 。我已经从 1 个主题(孩子)中生成了 1 个随机问题,我不能让其他 4 个主题有来自不同主题的 5 个问题 我的 Firebase 数据库在这里:

我的代码是:

final Query questionFromB = FirebaseDatabase.getInstance().getReference().child("B").orderByChild("questionID").equalTo(new Random().nextInt(15));
questionFromB.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot datasnapshot) {
        for (DataSnapshot question : datasnapshot.getChildren()){
            if (questionCount < 1){
                questionView.setText(question.getValue(QuizQuestions.class).getQuestionText());
                answerText.setText(question.getValue(QuizQuestions.class).getAnswerText());

                arrayList.add(new QuizQuestions(question.getValue(QuizQuestions.class).getQuestionID(),question.getValue(QuizQuestions.class).getQuestionText(),question.getValue(QuizQuestions.class).getAnswerText()));
                datasnapshot.getChildrenCount();
                questionCount ++;
            }else{
                nextButton.setVisibility(View.GONE);
                nextActivityButton.setVisibility(View.VISIBLE);
                for (int i=0;i<arrayList.size();i++){
                    textView.append(arrayList.get(i).getQuestionText());
                    textView.append(" \n ");
                    textView.append(" \n ");
                }

            }
        }
    }


    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        Toast.makeText(MainActivity.this, "Error, something was wrong", Toast.LENGTH_SHORT).show();
    }
});

(在第二个活动中,我只是在 TextView 中显示 ArrayList 内容)

【问题讨论】:

    标签: java android firebase firebase-realtime-database


    【解决方案1】:

    如果你想从多个查询中得到结果,那么你应该考虑使用Query#get(),它返回一个Task类型的对象。话虽如此,要在本地合并 5 个不同的 Firebase 实时数据库请求,我建议您使用 Tasks.whenAllSuccess() 方法。您可以使用以下代码行来实现这一点:

    Query questionFromA = FirebaseDatabase.getInstance().getReference()
            .child("A")
            .orderByChild("questionID")
            .equalTo(new Random()
            .nextInt(15));
    
    Query questionFromB = FirebaseDatabase.getInstance().getReference()
            .child("B")
            .orderByChild("questionID")
            .equalTo(new Random()
            .nextInt(15));
    
    //Create the other three queries.
    
    Task aTask = questionFromA.get();
    Task bTask = questionFromb.get();
    
    //Create the other three Task objects.
    
    Task combinedTask = Tasks.whenAllSuccess(aTask, bTask).addOnSuccessListener(new OnSuccessListener<List<Object>>() {
        @Override
        public void onSuccess(List<Object> list) {
             //Do what you need to do with your list
        }
    });
    

    如您所见,当覆盖“onSuccess()”方法时,结果是一个对象列表。最后,只需将列表中的每个对象映射成QuizQuestions 类型的对象,然后用它做你需要做的事情。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-07
      • 1970-01-01
      • 1970-01-01
      • 2021-03-11
      • 2019-10-19
      相关资源
      最近更新 更多