mardi 25 septembre 2018

need help to create random number for firebase data in quiz app

I am making an Quiz app wherein questions and answers are taken from firebase. I have uploaded questions with its options in firebase and now want to retrieve them randomly I have tried Random number = new Random(); number.nextInt(25); the problem is I dont need repeats I need one number to be generated only once and when questions end an alert bar should show and send user to score activity here is my main activity

   @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (mScore != 0){
    mScore = savedInstanceState.getInt("QK");

    }
    else{
        mScore = 0;

    }
    setContentView(R.layout.activity_bioquiz);
    setAndroidContext(this);
    choice1b = findViewById(R.id.choice1);
    choice2b = findViewById(R.id.choice2);
    choice3b = findViewById(R.id.choice3);
    choice4b = findViewById(R.id.choice4);
    mScoreTextView = findViewById(R.id.Score);
    mQuestionTextView = findViewById(R.id.questionTextView);

        Random m = new Random();

    int mQuestionNo = m.nextInt(25);



    updateQuestion();
    View.OnClickListener A = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (choice1b.getText().equals(mAnswer)) {
                choice1b.setBackgroundColor(Color.GREEN);
                mScore = mScore + 1;
                mScoreTextView.setText("Score "+String.valueOf(mScore) + "/"+ " 100");
                Toast.makeText(Bioquiz.this, "You got it!", Toast.LENGTH_SHORT).show();
                updateQuestion();
            } else {
                updateQuestion();
                v.setBackgroundColor(Color.RED);
                Toast.makeText(Bioquiz.this, "oops Wrong Answer", Toast.LENGTH_SHORT).show();
            }

        }
    };
    View.OnClickListener B = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (choice2b.getText().equals(mAnswer)) {
                choice1b.setBackgroundColor(Color.GREEN);
                mScore = mScore + 1;
                mScoreTextView.setText("Score "+String.valueOf(mScore) + "/"+ " 100");
                Toast.makeText(Bioquiz.this, "You got it!", Toast.LENGTH_SHORT).show();
                updateQuestion();
            } else {
                updateQuestion();
                v.setBackgroundColor(Color.RED);
                Toast.makeText(Bioquiz.this, "oops Wrong Answer", Toast.LENGTH_SHORT).show();

            }

        }

    };
    View.OnClickListener C = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (choice4b.getText().equals(mAnswer)) {
                choice1b.setBackgroundColor(Color.GREEN);
                mScore = mScore + 1;
                mScoreTextView.setText("Score "+String.valueOf(mScore) + "/"+ " 100");
                Toast.makeText(Bioquiz.this, "You got it!", Toast.LENGTH_SHORT).show();
                updateQuestion();
            } else {
                v.setBackgroundColor(Color.RED);
                updateQuestion();
                Toast.makeText(Bioquiz.this, "oops Wrong Answer", Toast.LENGTH_SHORT).show();
            }

        }
    };

    View.OnClickListener D = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (choice4b.getText().equals(mAnswer)) {
                choice1b.setBackgroundColor(Color.GREEN);
                mScore = mScore + 1;
                mScoreTextView.setText("Score "+String.valueOf(mScore) + "/"+ " 100");
                Toast.makeText(Bioquiz.this, "You got it!", Toast.LENGTH_SHORT).show();
                updateQuestion();

            } else {

                v.setBackgroundColor(Color.RED);
                updateQuestion();
                Toast.makeText(Bioquiz.this, "oops Wrong Answer", Toast.LENGTH_SHORT).show();
            }

        }
    };
    choice1b.setOnClickListener(A);
    choice2b.setOnClickListener(B);
    choice3b.setOnClickListener(C);
    choice4b.setOnClickListener(D);

}

    public void updateQuestion() {
        Random number = new Random();
        number.nextInt(25);



    Firebase questionRef = new Firebase("https://mcatbiologymcqs.firebaseio.com/"+mQuestionNo+"/question");
    questionRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String question = dataSnapshot.getValue(String.class);
            mQuestionTextView.setText(question);
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });
    Firebase choice1Ref = new Firebase("https://mcatbiologymcqs.firebaseio.com/" +mQuestionNo + "/choice1");
    choice1Ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String choice1 = dataSnapshot.getValue(String.class);
            choice1b.setText(choice1);
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });
    mChoice2Ref = new Firebase("https://mcatbiologymcqs.firebaseio.com/"+mQuestionNo+"/choice2");
    mChoice2Ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String choice2 = dataSnapshot.getValue(String.class);
            choice2b.setText(choice2);
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });
    mChoice3Ref = new Firebase("https://mcatbiologymcqs.firebaseio.com/"+mQuestionNo+"/choice3");
    mChoice3Ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String choice3 = dataSnapshot.getValue(String.class);
            choice3b.setText(choice3);
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });
    mChoice4Ref = new Firebase("https://mcatbiologymcqs.firebaseio.com/"+mQuestionNo+"/choice4");
    mChoice4Ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String choice4 = dataSnapshot.getValue(String.class);
            choice4b.setText(choice4);
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });
    Firebase answerRef = new Firebase("https://mcatbiologymcqs.firebaseio.com/"+mQuestionNo+"/answer");
    answerRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            mAnswer = dataSnapshot.getValue(String.class);

        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });

    if (mScoreTextView == null){

        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setTitle("Quiz Over");
        if (mScore ==20){alert.setMessage("Very Good");}
        else {
            alert.setMessage("You need to Work Hard");
        }
        alert.setPositiveButton("View Score", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
           Intent score = new Intent(Bioquiz.this,Score.class);

           startActivity(score);
            }
        });
        alert.setCancelable(false);
        alert.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
           finish();
            }
        });
        alert.show();


        }
    }

@Override
public void onBackPressed() {
    super.onBackPressed();
    Intent m = new Intent(Bioquiz.this,MainActivity.class);
    startActivity(m);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("SK",mScore);





}}

please help me with a solution. Noob here :(




Aucun commentaire:

Enregistrer un commentaire