本文介绍了随机活动,无重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这里阅读了数百篇有关它的文章,但没有一篇适合我的简单需求. NOOB警报.

I already read hundreds of posts here about it but none is adapted to my simple needs. NOOB ALERT.

我试图在我的应用中从活动列表中选择一个通知,并随机打开其中一个,但没有重复,我能够产生随机效果,但是某些活动会弹出很多次而其他活动却没有

I'm trying to have a notification in my app choose from a list of activities and open one of them randomly but without repetition, I was able to have a randomized effect but some activities pop up many times and others don't.

所以基本上,我需要随机浏览列表中的所有活动,并在访问了所有活动之后,重新整理列表并重新开始

So basically what I need is to go over all the activities on the list randomly, and when all of them have been visited, re-shuffle the list and start over

这是我做的一个小例子:一个按钮将我带到10项活动:

Here's a little example I cooked up: one button that takes me to 10 activities:

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 04 ,01

The results are like this:First run: 04, 07, 07, 05, 03, 09, 06, 09, 04, 01

第二次运行:07、04, 10 02 05 ,06, 10 05 02 ,08.

Second run: 07, 04, 10, 02, 05, 06, 10, 05, 02, 08.

我似乎找不到它.

谢谢.

推荐答案

男人,尝试这样的事情:

Look man, try something like this:

            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);

        }
    });

这篇关于随机活动,无重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 15:28