I'm trying to add objects to an ArrayList, but it seems that whenever I call the add method, the list is populated by only the last object that I add.
Here's how I add:
MyObject testObject1 = new MyObject();
testObject1.setType(0);
MyList.myList.add(testObject1);
MyObject testObject2 = new MyObject();
testObject2.setType(1);
MyList.myList.add(testObject2);
MyList is a class with a single ArrayList which is defined as follows:
public static ArrayList<MyObject> myList = new ArrayList<MyObject>();
MyObject is a class with some member variables and methods:
static int type;
public void setType(int inType) {
type = inType;
}
I then list out the objects in MyList.myList as follows:
for (int i=0; i<MyList.myList.size(); i++) {
Log.d(TAG, "Type is " + MyList.myList.get(i).getType());
}
It lists 2 objects, but the type is always 1.
What's up?
Thanks.