1

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.

4
  • 2
    ArrayList allows duplicate values. Provide your full code. You are doing something wrong. Commented Aug 19, 2014 at 7:32
  • Among other things, you don't show how you're sharing that list with the UI thread, and whether you have proper synchronization. Commented Aug 19, 2014 at 7:33
  • 3
    @MachOSX is that your issue? ArrayList not guaranteed insertion order. When you add a new element order of element may changed. If you want to keep insertion order use LinkedList Commented Aug 19, 2014 at 7:40
  • 1
    My guess: field gid of class SHMapTile is static. Is it? Can you share the code for that class? Commented Aug 19, 2014 at 7:40

1 Answer 1

2

As you said in comments, field gid of class SHMapTile is static.

That means that the same value is shared between all instances of the class. If you update one, it will be updated in all istances of that class. Remove the static modifier and it will work

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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