1

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.

2
  • where do you call all these methods? what is your onCreate()? Commented Mar 12, 2012 at 13:22
  • use this classes.remove(crazy) instead of classes.remove(cheese) because remove method take an index of item see sun java docs Commented Mar 12, 2012 at 13:25

2 Answers 2

1

I guess your remove is working but each Activity you create contains a new instance of classes and that's not what you want. It might work this way.

public class ActivityHelper extends Activity{
static ArrayList<String> classes = new ArrayList<String>();
static {
    classes.add("Activity1");
    classes.add("Activity2");
    classes.add("Activity3");
            //etc
}
public  String openClass (){
    // don't add classes here - would be done every time.
    // print out some debug
    Log.d("ActivityHelper", "State before openClass: " + Arrays.toString(classes.toArray()));
    Random rand = new Random ();
    int crazy = rand.nextInt(classes.size());
    String cheese = classes.get(crazy);
    classes.remove(crazy);
    // print out some debug
    Log.d("ActivityHelper", "State after openClass: " + Arrays.toString(classes.toArray()));
    return cheese;
} 
Sign up to request clarification or add additional context in comments.

3 Comments

Hi tried this method but still it seems to be creating a new Instance of the class. Also tried removing via index but still same case. I am hoping to work around it and try a ArrayList of Classes and randomly cycle through them if possible. Any thoughts?
if you print (e.g. Log.d("TAG", Arrays.toString(classes.toArray()));) the content of classes in openClass is the content really reset all the time? Because if classes is static then it will exist only once for all instances of your Activity. That will not work if your Activities are in different apps or processes though. And a restart of your app (kill with some TaskManager) might reset classes as well.
@danpasco I've added some debug to the Answer. It should print a list that always gets smaller. If not you might need to use e.g. SharedPreferences to keep a list of Activities you still want to be opened.
0

ArrayList.remove() expects an Integer, not a class. Call .remove() with the Integer index you want to remove and it should work as expected.

3 Comments

remove() is overloaded. remove(int) and remove(Object)
@SergeyBenner: hi,can you give me an example where we pass an object in ArrayList.remove(int index) apart from Integer Object.
By bad, didn't see that it accepted Object as a parameter. Either wa try removing by index? Clearly not working for Object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.