Collections.shuffle(list) 不随机排列我的列表
Collections.shuffle(list) doesn't shuffle my list
我的测验应用程序有问题。我必须整理我的问题列表(取自 Firebase 数据库),但我的代码似乎是错误的。
文件Start.java:
private void loadQuestion(String categoryId) {
//First, clear list if have old question
if(Common.questionList.size() > 0)
Common.questionList.clear();
questions.orderByChild("CategoryId").equalTo(categoryId)
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot : dataSnapshot.getChildren())
{
Question ques = postSnapshot.getValue(Question.class);
Common.questionList.add(ques);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
//Random list
Collections.shuffle(Common.questionList);
}
和文件 Common.java:
public class Common {
public static String categoryId;
public static User currentUser;
public static List<Question> questionList = new ArrayList<>();
}
谢谢大家
正在通过回调添加您的数据addValueEventListener
在你的函数开始时,你清除了你的列表:
Common.questionList.clear();
所以 Collections.suffle
只在你的空列表上洗牌
您想要的是将随机播放函数放入您的回调中:
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot : dataSnapshot.getChildren())
{
Question ques = postSnapshot.getValue(Question.class);
Common.questionList.add(ques);
}
// Shuffle it
Collections.shuffle(Common.questionList);
}
我的测验应用程序有问题。我必须整理我的问题列表(取自 Firebase 数据库),但我的代码似乎是错误的。
文件Start.java:
private void loadQuestion(String categoryId) {
//First, clear list if have old question
if(Common.questionList.size() > 0)
Common.questionList.clear();
questions.orderByChild("CategoryId").equalTo(categoryId)
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot : dataSnapshot.getChildren())
{
Question ques = postSnapshot.getValue(Question.class);
Common.questionList.add(ques);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
//Random list
Collections.shuffle(Common.questionList);
}
和文件 Common.java:
public class Common {
public static String categoryId;
public static User currentUser;
public static List<Question> questionList = new ArrayList<>();
}
谢谢大家
正在通过回调添加您的数据addValueEventListener
在你的函数开始时,你清除了你的列表:
Common.questionList.clear();
所以 Collections.suffle
只在你的空列表上洗牌
您想要的是将随机播放函数放入您的回调中:
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot : dataSnapshot.getChildren())
{
Question ques = postSnapshot.getValue(Question.class);
Common.questionList.add(ques);
}
// Shuffle it
Collections.shuffle(Common.questionList);
}