2011-01-07 129 views
1

我正在閱讀資源中的xml文件,該文件包含旅行社的位置和地址列表,並且我試圖在解析列表中的列表後將該列表與地圖一起使用。因此,每次使用agencies.add(agency)時,它都會將其添加到數組中,但也會使用新值更改所有以前的項目。這裏是我的代碼,如果有人能幫助或解釋:ArrayList出現問題

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(); 
    } 
} 

謝謝

回答

3

您只創建了,你每次都重用一個Agency對象。一個ArrayList只是一個對象的引用數組,在這種情況下,你只是不斷地重複添加同一個對象。每次要將其添加到ArrayList時,都需要創建一個新的Agency對象。

我不是那熟悉的XMLParser語法,但我認爲,如果你改變:

Agency agency = new Agency(); 

agency = new Agency(); 

else if(eventType == XmlPullParser.END_TAG) 

它應該修復它。

+0

簡單可靠的解決方案,非常感謝,它修復了我的問題。 – Houssem 2011-01-07 11:14:16