Problem with adding same object to ArrayList

Put your problem here if it does not fit any of the other categories.

Problem with adding same object to ArrayList

Postby lesmond » Fri Jul 02, 2010 9:57 am

Hello, I have just started Android development and think I am making good progress but keep stumbling across simple things!

I have the following code but the ArrayList I have is populated with the correct total of "Ship" objects but always it puts the last object parsed!

Help please!

Lee
Code: Select all
package com.testing.xmltest;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;


public class ExampleHandler extends DefaultHandler{

   // ===========================================================
   // Fields
   // ===========================================================
   
   private boolean in_markers = false;
   private boolean in_marker = false;
   
   private Ship myShip = new Ship();
   private ArrayList<Ship> shipArray;

   // ===========================================================
   // Getter & Setter
   // ===========================================================


   public Ship getShip(){
      return this.myShip;   
   }
        
   public ArrayList<Ship> getShipArray(){
      return this.shipArray;   
   }

   // ===========================================================
   // Methods
   // ===========================================================
   @Override
   public void startDocument() throws SAXException {
      
      this.shipArray = new ArrayList<Ship>();
      this.myShip = new Ship();
   }

   @Override
   public void endDocument() throws SAXException {
      Log.i("Array Count", ""+ this.shipArray.size());
   }

   
   @Override
   public void startElement(String namespaceURI, String localName,
         String qName, Attributes atts) throws SAXException {
      
      
      if (localName.equals("markers")) {
         this.in_markers = true;
      }else if (localName.equals("marker")) {
         //Log.v(localName, atts.getValue("name"));
         myShip.setShipName(atts.getValue("name"));
         this.in_marker = true;
      }
   }
   
   
   @Override
   public void endElement(String namespaceURI, String localName, String qName)
         throws SAXException {
      if (localName.equals("markers")) {
         this.in_markers = false;
      }else if (localName.equals("marker")) {
         this.in_marker = false;
         Ship tempAddShip = new Ship();
         tempAddShip = this.myShip;
         Log.i("Adding...",tempAddShip.getShipName());
         //shipArray.add(myShip);
         this.shipArray.add(tempAddShip);
         
      }
   }
   
   
   @Override
    public void characters(char ch[], int start, int length) {
      
    }
}
lesmond
Junior Developer
Junior Developer
 
Posts: 11
Joined: Thu Jul 01, 2010 7:26 am

Top

Re: Problem with adding same object to ArrayList

Postby anarche » Sat Jul 03, 2010 1:14 am

I don't understand you question.

The last object in the ArrayList is the only one parsed? Or the first one parsed?
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Re: Problem with adding same object to ArrayList

Postby lesmond » Sat Jul 03, 2010 7:04 am

I fixed it by moving the
this.myShip = new Ship();

Into the startElement....
lesmond
Junior Developer
Junior Developer
 
Posts: 11
Joined: Thu Jul 01, 2010 7:26 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 22 guests