I've already checked out other people who were facing the same issue, & almost all of them were instantiating the objects they want to add outside their loop. I'm already doing that inside my while loop, so I'm guessing that's not my issue.
public ArrayList<SHMapTile> tiles = new ArrayList<SHMapTile>();
.
.
.
private void parseXML(XmlPullParser parser) throws XmlPullParserException, IOException
{
int eventType = parser.getEventType();
int i = 0;
while ( eventType != XmlPullParser.END_DOCUMENT )
{
String name = null;
switch ( eventType )
{
case XmlPullParser.START_TAG:
{
name = parser.getName();
if ( name.equalsIgnoreCase("tile") )
{
SHMapTile tile = new SHMapTile();
tile.gid = i;
tiles.add(tile);
System.out.println("gid: " + tiles.get(0).gid);
i++;
}
break;
}
case XmlPullParser.END_TAG:
{
name = parser.getName();
if ( name.equalsIgnoreCase("data") )
{
this.runOnUiThread(new Runnable() {
public void run() {
drawCurrentMapRegion();
}
});
}
break;
}
}
eventType = parser.next();
}
}
For debugging, I attached a counter to each new object. When I print out the counter of the first element in the list, it's always different. I tried cloning. Didn't work either.
ArrayListnot guaranteed insertion order. When you add a new element order of element may changed. If you want to keep insertion order useLinkedListgidof classSHMapTileisstatic. Is it? Can you share the code for that class?