i have a problen with random in arrays. It is like a quiz and i have arrays with questions,choises and answers. When i start my quiz , qustions are repeating Know about shuffle but do not know how to use it i have a method:
private void updateQuestion(int num){
question.setText(mQuestionAvtor.getQuestion(num));
answer1.setText(mQuestionAvtor.getChoice1(num));
answer2.setText(mQuestionAvtor.getChoice2(num));
answer3.setText(mQuestionAvtor.getChoice3(num));
answer4.setText(mQuestionAvtor.getChoice4(num));
mAnswear= mQuestionAvtor.getCorrectAnswear(num);
}
It updates my questions from another Class with arrays by a num. This num i did by random :
updateQuestion(r.nextInt(mQuestionslength));
Example of updating questions
answer1 = (Button) findViewById(R.id.answear1);
answer2 = (Button) findViewById(R.id.answear2);
answer3 = (Button) findViewById(R.id.answear3);
answer4 = (Button) findViewById(R.id.answear4);
question = (TextView) findViewById(R.id.question);
n = r.nextInt(mQuestionslength);
while (n == n_before)
n = r.nextInt(mQuestionslength);
n_before = n;
updateQuestion(n);
answer1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(answer1.getText()==mAnswear){
mScore++;
mScorecurrent.setText("Рахунок: "+mScore);
}
n = r.nextInt(mQuestionslength);
while (n == n_before) n = r.nextInt(mQuestionslength);
n_before = n;
updateQuestion(n);
}
});
I need to do it in a such way , that r will not repeat. How can I do it? Thanks in advance!
answer1.getText()==mAnswearwill not work as this is a check for reference to a common object. ForStringcomparison, usemAnswear.equals(answer1.getText()).