Problem with ArrayList

Posted by Houssem on Stack Overflow See other posts from Stack Overflow or by Houssem
Published on 2011-01-07T10:40:53Z Indexed on 2011/01/07 10:53 UTC
Read the original article Hit count: 205

Filed under:
|

Hello, I'm reading an xml file from resources, that file contains a list of travel agencies location and adresses and i'm trying to put this list after parsing in an arraylist to use it with maps. So each time I use agencies.add(agency) it adds it to the array but also changes all previous items with the new value. Here's my code if someone can help or explain :

public class Main extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      TextView myXmlContent = (TextView)findViewById(R.id.my_xml);
      String stringXmlContent;
 try {
  stringXmlContent = getEventsFromAnXML(this);
  myXmlContent.setText(stringXmlContent);
 } catch (XmlPullParserException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
  }
  boolean na=false;
  List<Agency> agencies = new ArrayList();
  Agency agency=new Agency();
  int i=0;
  private String getEventsFromAnXML(Activity activity)
  throws XmlPullParserException, IOException
  {
   StringBuffer stringBuffer = new StringBuffer();
   Resources res = activity.getResources();
   XmlResourceParser xpp = res.getXml(R.xml.hotels);
   xpp.next();
   int eventType = xpp.getEventType();

   while (eventType != XmlPullParser.END_DOCUMENT)
   {
    if(eventType == XmlPullParser.START_DOCUMENT)
    {
     stringBuffer.append("--- Start XML ---");
    }
    else if(eventType == XmlPullParser.START_TAG)
    {
        if (xpp.getName().equals("DataBase")){
            agency.ResetTsp();
            String name=xpp.getAttributeValue(null, "name");
            agency.setTspTitle(name);
            na=true;
            stringBuffer.append("\nAgence : "+ name);
        }
        if (xpp.getName().equals("Title")){
            xpp.next();
            agency.setTitle(xpp.getText());
            stringBuffer.append("\nFiliale: "+xpp.getText());
            xpp.nextTag();
        }
        if (xpp.getName().equals("Address")){
            xpp.next();
            agency.setAddress(xpp.getText());
            stringBuffer.append("\nAdresse: "+xpp.getText());
            xpp.nextTag();
        }

        if (xpp.getName().equals("Phone") && na==true){
            xpp.next();
            agency.setTspPhone(xpp.getText());
            stringBuffer.append("\nPhone: "+xpp.getText());
            xpp.nextTag();
        }else{
            if (xpp.getName().equals("Phone") && na==false){
                xpp.next();
                agency.setPhone(xpp.getText());
                stringBuffer.append("\nPhone: "+xpp.getText());
                xpp.nextTag();
            }

        }

        if (xpp.getName().equals("Fax")){
            xpp.next();
            agency.setFax(xpp.getText());
            stringBuffer.append("\nFax: "+xpp.getText());
            xpp.nextTag();
        }

        if (xpp.getName().equals("e-Mail")){
            xpp.next();
            agency.setMail(xpp.getText());
            stringBuffer.append("\ne-Mail: "+xpp.getText());
            xpp.nextTag();
        }
        if (xpp.getName().equals("Latitude")){
            xpp.next();
            agency.setLatitude(Double.parseDouble(xpp.getText()));
            stringBuffer.append("\nLatitude: "+xpp.getText());
            xpp.nextTag();
        }
        if (xpp.getName().equals("Longitude")){
            xpp.next();
            agency.setLongitude(Double.parseDouble(xpp.getText()));
            stringBuffer.append("\nLongitude: "+xpp.getText());
        }


    }
    else if(eventType == XmlPullParser.END_TAG)
    {
        if (xpp.getName().equals("DataBase") || xpp.getName().equals("Agency")){
            agencies.add(i,agency);
            i=i+1;
            Agency agency = new Agency();
        }
    }


    eventType = xpp.next();
   }
   stringBuffer.append("\n--- End XML ---");
   return stringBuffer.toString();
  }
}

thank you

© Stack Overflow or respective owner

Related posts about android

Related posts about arraylist