如何从Firebase数据库中随机播放一个值并显示有限的值?

时间:2018-10-04 19:51:04

标签: android firebase random firebase-realtime-database shuffle

我已经开发了一个测验应用程序。我有五个问题要显示。我使用Firebase。 现在,程序运行正常。但是我需要每次都将这些问题打乱,我只想在一次会议上展示三个问题。即是否有50个问题。我希望用户回答那50个问题中的任何10个。这些问题应该是随机的。我需要代码,因为我是android编程的新手。

GameActivity.java

 import android.content.Intent;
 import android.os.CountDownTimer;
 import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.widget.Button;
 import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

 import com.blackevil.oneandonlythala.Common.Common;
import com.blackevil.oneandonlythala.R;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
 import com.squareup.picasso.Picasso;

 import android.view.View;


 public class GameActivity extends AppCompatActivity implements View.OnClickListener {

final static long INTERVAL=1000;
final static long TIMEOUT=7000;
int progressValue=0;
CountDownTimer mCountDown;
int index=0,score=0,thisQuestion=0,totalQuestion,correctAnswer;
//Firebase

ProgressBar progressbar;
ImageView question_image;
Button btnA,btnB,btnC,btnD;
TextView txtScore,txtQuestionNum,question_txt;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);





    txtScore=findViewById(R.id.txtScore);
    txtQuestionNum=findViewById(R.id.txtTotalQuestion);
    question_txt=findViewById(R.id.question_text);
    question_image=findViewById(R.id.question_image);


    progressbar=findViewById(R.id.progressBar);


    btnA=findViewById(R.id.btnAnswerA);
    btnB=findViewById(R.id.btnAnswerB);
    btnC=findViewById(R.id.btnAnswerC);
    btnD=findViewById(R.id.btnAnswerD);


    btnA.setOnClickListener( this);
    btnB.setOnClickListener(this);
    btnC.setOnClickListener(this);
    btnD.setOnClickListener(this);


}

@Override
public void onClick(View v) {

    mCountDown.cancel();
    if(index< totalQuestion) //still have question in list
    {
        Button clickedButton=(Button)v;
        if(clickedButton.getText().equals(Common.questionList.get(index).getCorrectAnswer()))
        {
            score=score+10;
            correctAnswer++;
            showQuestion(++index);
        }
        else
        {
            Intent intent=new Intent(GameActivity.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();
            }

            txtScore.setText(String.format("%d",score));
    }

}

private void showQuestion(int i) {
    if(index<totalQuestion)
    {
        thisQuestion++;
        txtQuestionNum.setText(String.format("%d / %d",thisQuestion,totalQuestion));
        progressbar.setProgress(0);
        progressValue=0;

        if(Common.questionList.get(index).getIsImageQuestion().equals("true"))
        {
            Picasso.with(getBaseContext())
                    .load(Common.questionList.get(index).getQuestion())
                    .into(question_image);
            question_image.setVisibility(View.VISIBLE);
            question_txt.setVisibility(View.INVISIBLE);

        }
        else
        {
            question_txt.setText(Common.questionList.get(index).getQuestion());
            question_image.setVisibility(View.INVISIBLE);
            question_txt.setVisibility(View.VISIBLE);
        }

        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
    {
        Intent intent=new Intent(GameActivity.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();
    }



}


protected  void onResume()
{
    super.onResume();
    totalQuestion=Common.questionList.size();

    mCountDown=new CountDownTimer(TIMEOUT,INTERVAL) {
        @Override
        public void onTick(long minisec) {
            progressbar.setProgress(progressValue);
            progressValue++;

        }

        @Override
        public void onFinish() {
            mCountDown.cancel();
            showQuestion(++index);

        }
    };
    showQuestion(index);
}
}

1 个答案:

答案 0 :(得分:1)

您应该在客户端下载所有数据。使用map将其存储为Array,然后将其改组为该数组。所有这些都可以从客户端完成。