0

I have developed a quiz app. I have five questions to display. I use Firebase. now, the program is running fine. But I need those questions to be shuffled everytime and I want to show only three questions at a session. i.e. if there a 50 questions. I want the user to answer for any 10 question from that 50. the questions should be in random. I need the code since I am a newbie in android programming.

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);
}
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • There is no built-in operation to select a random child node from the database. You can either download all questions and select them client-side, or add some data to your database to allow selecting a random item. For a general approach, I highly recommend Dan's answer here: https://stackoverflow.com/a/46801925 Aside from that, see https://stackoverflow.com/q/45145596, https://stackoverflow.com/a/40767172, https://stackoverflow.com/a/40853780, https://stackoverflow.com/a/51539748 – Frank van Puffelen Oct 04 '18 at 20:12
  • Check also **[this](https://stackoverflow.com/questions/50413117/how-to-get-unique-random-product-in-node-firebase/50413208)** out. – Alex Mamo Oct 05 '18 at 09:02

1 Answers1

1

You should download all the data in the client side. Use map to store it as an Array and then shuffle into this array. All of this can be done from the client side.

Christophe Chenel
  • 1,802
  • 2
  • 15
  • 46
  • beated me on the chrono here haha. Maybe you can add an example of a shuffle method for your answer to be a little more complete – Sirmyself Oct 04 '18 at 20:16
  • yes add some piece of code for your answer. that would be more helpful –  Oct 05 '18 at 04:39