我是一名老师,为学生的休息做一个测验已完成。我在Firebase实时数据库中有 500个问题 ,我要做的就是 50个问题>随机重复个问题,并在50个问题之后 设置听众 显示结果
这是我的测验活动
public class grandtest extends AppCompatActivity {
TextView mQuestionTextView;
ProgressBar mProgressBar;
Button bchoice1,bchoice2,bchoice3,bchoice4;
Random Rand;
String mAnswer;
Timer mTimer;
TextView mScoreTextView;
public static int mScore;
int mQuestionNo = 0;
private Firebase mQuestionRef;
private Firebase mChoice1Ref;
private Firebase mChoice2Ref;
private Firebase mChoice3Ref;
private Firebase mChoice4Ref;
private Firebase mAnswerRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grandtest);
mQuestionTextView = findViewById(R.id.mquestiontextview);
mProgressBar = findViewById(R.id.progressBar);
bchoice1 = findViewById(R.id.choice1);
bchoice2 = findViewById(R.id.choice2);
bchoice3 = findViewById(R.id.choice3);
bchoice4 = findViewById(R.id.choice4);
mScoreTextView = findViewById(R.id.mScore);
}
public void updateQuestion (){
mQuestionRef = new Firebase("https://class9notes-2808b.firebaseio.com/"+mQuestionNo +"/question");
mQuestionRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String question = dataSnapshot.getValue(String.class);
mQuestionTextView.setText(question);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
Toast.makeText(grandtest.this,"Please enable data",Toast.LENGTH_SHORT).show();
}
});
mChoice1Ref = new Firebase("https://class9notes-2808b.firebaseio.com/"+mQuestionNo+"/choice1");
mChoice1Ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String choice1 = dataSnapshot.getValue(String.class);
bchoice1.setText(choice1);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
mChoice2Ref = new Firebase("https://class9notes-2808b.firebaseio.com/"+mQuestionNo+"/choice2");
mChoice2Ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String choice2 = dataSnapshot.getValue(String.class);
bchoice2.setText(choice2);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
mChoice3Ref = new Firebase("https://class9notes-2808b.firebaseio.com/"+mQuestionNo+"/choice3");
mChoice3Ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String choice3 = dataSnapshot.getValue(String.class);
bchoice3.setText(choice3);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
mChoice4Ref = new Firebase("https://class9notes-2808b.firebaseio.com/"+mQuestionNo+"/choice4");
mChoice4Ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String choice4 = dataSnapshot.getValue(String.class);
bchoice4.setText(choice4);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
mAnswerRef = new Firebase("https://class9notes-2808b.firebaseio.com/"+mQuestionNo+"/answer");
mAnswerRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
mAnswer = (String) dataSnapshot.getValue(String.class);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
mQuestionNo ++;
我想将mQuestionNo对象随机化,以每次检索随机问题。 这是我的Firebase数据的结构
答案 0 :(得分:1)
这不会解决您的问题,但是可以帮助您获得更好的编码! :)
创建一个包含您的问题的课程
public class Question {
String question;
String choice1;
String choice2;
String choice3;
String choice4;
String answer;
}
然后在您的活动中,您只能调用一个数据库引用,而不是多个。
int questionNo = 0;
mDatabase.child(String.valueOf(questionNo)).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Question question = new Question();
question.answer = dataSnapshot.child("answer").getValue().toString();
question.choice1 = dataSnapshot.child("choice1").getValue().toString();
question.choice2 = dataSnapshot.child("choice2").getValue().toString();
question.choice3 = dataSnapshot.child("choice3").getValue().toString();
question.choice4 = dataSnapshot.child("choice4").getValue().toString();
question.question = dataSnapshot.child("question").getValue().toString();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
这里我们从0索引处获取问题。
您可以像这样获得50个随机密钥:
ArrayList<Integer> questions = new ArrayList<>();
while(questions.size() < 50){
int random = new Random().nextInt(500 + 1) + 1;
if (!questions.contains(random)){
questions.add(random);
//Question added
}else{
//Question already added to list
}
}
现在,您有了要遍历的问题的键。然后,您可以调用questions.get(49)来获取所有键中的最后一个问题,使用该索引检查您的firebase,当firebase收到问题时,可以使用问题questions.remove(49)删除该索引。并转到下一个索引。当然,不应对49进行硬编码,而应将您设置为intquestionCount = 49的值设置为硬编码。然后,当您完成所有这些操作后,您将--questionCount;
这现在有点伪,但希望它可以给您一些想法:)
答案 1 :(得分:0)
答案 2 :(得分:0)
您可以创建一个存储先前问题的集合,并使用以下方法检索下一个问题编号。
Set<Integer> set = new HashSet<>();
public int getNextQuestionNumber() {
int num = (int) (Math.random() * 500); //Total number of questions in the database.
if (set.contains(num)) {
return getNextQuestionNumber();
} else {
set.add(num);
return num;
}
}