我已经在这里阅读了数百篇关于它的帖子,但没有一篇能够适应我的简单需求。 NOOB ALERT 。
我正在尝试在我的应用程序中选择一个活动列表并随机打开其中一个但没有重复,我能够有一个随机效果,但有些活动弹出多次而其他活动没有
基本上我需要的是随机查看列表中的所有活动,当所有活动都被访问后,重新洗牌并重新开始
这是我做的一个小例子:一个按钮带我参加10项活动:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button randomBtn;
randomBtn = (Button) findViewById(R.id.randomBtn);
randomBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ArrayList<Class> activities = new ArrayList<>();
activities.add(Activity01.class);
activities.add(Activity02.class);
activities.add(Activity03.class);
activities.add(Activity04.class);
activities.add(Activity05.class);
activities.add(Activity06.class);
activities.add(Activity07.class);
activities.add(Activity08.class);
activities.add(Activity09.class);
activities.add(Activity10.class);
Collections.shuffle(activities);
Intent intent = new Intent(MainActivity.this, activities.get(0));
startActivity(intent);
}
});
}
}
结果是这样的: 首次运行: 04 , 07 , 07 ,05,03, 09 ,06, 09 < / strong>, 04 ,01
第二轮:07,04, 10 , 02 , 05 ,06, 10 , 05 , 02 ,08。
我似乎无法找到它。
提前谢谢。
答案 0 :(得分:1)
看看男人,尝试这样的事情:
ArrayList<Class> activities = new ArrayList<>();
activities.add(Activity01.class);
activities.add(Activity02.class);
activities.add(Activity03.class);
activities.add(Activity04.class);
activities.add(Activity05.class);
activities.add(Activity06.class);
activities.add(Activity07.class);
activities.add(Activity08.class);
activities.add(Activity09.class);
activities.add(Activity10.class);
Button randomBtn;
randomBtn = (Button) findViewById(R.id.randomBtn);
randomBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(activities.size == 0){
//create a method that instantiate again the Activities and add to the list;
}
Collections.shuffle(activities);
Activity activityToShow = activities.get(0);
Intent intent = new Intent(MainActivity.this, activityToShow);
activities.remove(0);
startActivity(intent);
}
});
答案 1 :(得分:0)
为了将来参考,这里是我使用的代码,感谢@PedroFernandes,并适用于我的情况:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayList<Class> activities = new ArrayList<>();
activities.add(Activity01.class);
activities.add(Activity02.class);
activities.add(Activity03.class);
activities.add(Activity04.class);
activities.add(Activity05.class);
activities.add(Activity06.class);
activities.add(Activity07.class);
activities.add(Activity08.class);
activities.add(Activity09.class);
activities.add(Activity10.class);
Button randomBtn;
randomBtn = (Button) findViewById(R.id.randomBtn);
randomBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (activities.size()==0){
activities.add(Activity01.class);
activities.add(Activity02.class);
activities.add(Activity03.class);
activities.add(Activity04.class);
activities.add(Activity05.class);
activities.add(Activity06.class);
activities.add(Activity07.class);
activities.add(Activity08.class);
activities.add(Activity09.class);
activities.add(Activity10.class);
}
Collections.shuffle(activities);
Class activityToShow = activities.get(0);
Intent intent = new Intent(MainActivity.this, activityToShow);
activities.remove(0);
startActivity(intent);
}
});
}
}