Hi I am a bit new to Java and Android. I am working on a project which it cycles through random Activities and I am stuck with a problem for days.
public class ActivityHelper extends Activity {
ArrayList<String> classes = new ArrayList<String>();
public void addClass (){
classes.add("Activity1");
classes.add("Activity2");
classes.add("Activity3");
//etc
}
public String openClass (){
addClass();
Random rand = new Random ();
int crazy = rand.nextInt(classes.size());
String cheese = classes.get(crazy);
classes.remove(cheese);
return cheese;
}
}
// A Method in Activity1 extends ActivityHelper
public void doJob() {
String cheese = openClass();
try {
@SuppressWarnings("rawtypes")
Class ourClass = Class.forName("com.activity.dan." + cheese);
Intent ourIntent = new Intent(Activity1.this, ourClass);
startActivity(ourIntent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
The program perfectly cycles through the classes but my problem is the classes.remove(cheese); doesnt work. I am hoping once the Class is opened it will be removed from the ArrayList and wont be used again if the doJob(); method is called on another class. I tried using static and putting the remove list in the Activity1 but nothing seems to work. Your help will be appreciated.
onCreate()?classes.remove(crazy)instead of classes.remove(cheese) because remove method take an index of item see sun java docs