我正在制作一个测验应用程序,其中的问题和答案均来自Firebase。我在Firebase中上传了带有选项的问题,现在想随机检索它们。我尝试过
Random number = new Random();
number.nextInt(25);
问题是我不需要重复。我只需要生成一个号码,问题结束时,警报栏就会显示出来,并向用户评分活动。这是我的主要活动
@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);
}}
答案 0 :(得分:1)
似乎您正在尝试从25个可能的问题中随机抽取一个问题,并且不想重复获得同一问题。您可以保留一个问题编号列表,并在用户看到该问题后将其从列表中删除。
//declare the list at the top with the other variables
public List<Integer> questionNos = new ArrayList<>();
在onCreate中更改此行
int mQuestionNo = m.nextInt(25);
这些行
for (int i = 0; i < numberOfQuestions; i++) {
questionNos.add(i);
}
Random r = new Random();
int index = r.nextInt(questionNos.size()-1);
int mQuestionNo = questionNos.remove(index);
在updateQuestion()中更改这些行
Random number = new Random();
number.nextInt(25);
对此
Random r = new Random();
int index = r.nextInt(questionNos.size()-1);
int number = questionNos.remove(index);
那应该可以帮助您实现大部分目标。您可能需要将列表保存在onPause中,然后将其加载到onResume中,以了解应用停止或旋转的时间。
答案 1 :(得分:-1)
如果要生成唯一身份,则可以使用System.currentTimeMillis()
代替number = new Random(); number.nextInt(25);
示例:
//Random m = new Random();
long mQuestionNo = System.currentTimeMillis();