简体   繁体   English

使用选项从 firebase 实时数据库中检索 mcq 数据

[英]Retrieve mcq data from firebase realtime database with options

I want to retrieve my mcq from firebase realtime database in my android app.我想从我的 android 应用程序中的 firebase 实时数据库中检索我的 mcq。

Below is the code for the same.下面是相同的代码。

databaseReference = FirebaseDatabase.getInstance().getReference().child("Questions").child(String.valueOf(computerCount));
                 
                databaseReference.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        final Question question = dataSnapshot.getValue(Question.class);
                        questionTxt.setText(question.getQuestion());
                        b1.setText(question.getOption1());
                        b2.setText(question.getOption2());
                        b3.setText(question.getOption3());
                        b4.setText(question.getOption4());


                        b1.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if(b1.getText().toString().equals(question.answer))
                                {
                                    Toast.makeText(getApplicationContext(),"Correct answer",Toast.LENGTH_SHORT).show();
                                    b1.setBackgroundColor(Color.GREEN);
                                    correct = correct +1;
                                    Handler handler = new Handler();

                                    handler.postDelayed(new Runnable() {
                                        public void run() {
                                            b1.setBackgroundColor(Color.parseColor("#03A9F4"));
                                            updateQuestion();
                                        }
                                    }, 1500);

                                }

I want this to fetch in random way instead of sequential way ex: 1,2我希望它以随机方式而不是顺序方式获取,例如:1,2

my json is like:我的 json 是这样的:
结构

you can retrieve all of your mcq's and saved them in an arraylist and later using您可以检索所有 mcq 并将它们保存在 arraylist 中,稍后使用

Collections.shuffle(arrayList);

you can shuffle them.你可以洗牌。 To know more about shuffle and usage you can follow this link要了解有关随机播放和用法的更多信息,您可以点击此链接

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

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