开发者

Problem with ArrayList

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("\开发者_C百科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


You have only created one Agency object that you are reusing each time. An ArrayList is just an array of references to objects, and in this case you just keep adding the same object over and over again. You need to create a new Agency object each time you want to add it to your ArrayList.

I'm not that familiar with the XMLParser syntax, but I think if you change:

 Agency agency = new Agency();

to

agency = new Agency();

within

else if(eventType == XmlPullParser.END_TAG)

it should fix it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜