jeudi 4 octobre 2018

How to shuffle a value from firebase database and show limited values?

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);
}
}




Aucun commentaire:

Enregistrer un commentaire