在使用arraylist进行随机化活动时,如何重新填充arraylist

时间:2016-06-05 13:24:12

标签: android android-intent arraylist

所以我真的很难创造一个游戏而且我一直想出路障。我将每个级别作为一个单独的活动,我从stackoverflow用户Rohan获得帮助,随机化每个活动的开始方式。现在为了避免内存不足问题,我必须使用finish()Android:noHistory=true ...我遇到的问题是,当游戏随机播放所有活动一次,并且arraylist变空时,我不知道重新填充活动的代码,因为每项活动的finish()Android:noHistory=true代码都会清空这些活动。

MyActivity.java

    import android.app.Application;


    public class MyApplication extends Application {

@Override
public void onCreate() {
    super.onCreate();
    ActivityList.init(23);
}}

ActivityList.java

    import java.util.ArrayList;
    import java.util.Collections;


    public class ActivityList {

   private static ActivityList instance;

    private final ArrayList<Class> activities;
    public final int numActivities;

   public static void init(int numActivities) {
    if (instance == null)
        instance = new ActivityList(numActivities);
  }

    private ActivityList(int numActivities) {
    this.numActivities = numActivities;
    activities = new ArrayList<>(numActivities);

        try {
             for(int i = 1 ; i <= numActivities ; i++) {
                 activities.add(Class.forName   ("com.game.thegame.Q" + i));
        }
    } catch(ClassNotFoundException e) {
        e.printStackTrace();
    }

    Collections.shuffle(activities);
}

public static ActivityList getInstance() {
    return instance;
}

public Class getRandomActivity() {
    if (activities.isEmpty()) {  //(I think I should add something here)

        return EndScreen.class;

    }

    Class clazz = activities.get(0);
    activities.remove(0);

    return clazz;
   }
        }

Q1.class

      Public void onbuttonClick(View v){
      Class activityToShow = ActivityList.getInstance().getRandomActivity();

    Intent intent = new Intent(this, activityToShow);
    intent.putExtra("score", score);

    startActivity(intent);}

1 个答案:

答案 0 :(得分:0)

你能不能只调用ActivityList.init(23);再次像你在开始时做的那样?