我正在从资源中读取一个xml文件,该文件包含旅行社位置和地址的列表,我试图在一个arraylist中解析之后将此列表用于地图。因此,每次我使用agencies.add(代理)时,它都会将其添加到数组中,但也会使用新值更改所有以前的项目。如果有人可以提供帮助或解释,这是我的代码:
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();
}
}
谢谢
答案 0 :(得分:3)
您每次只创建一个Agency
对象。 ArrayList
只是对象的引用数组,在这种情况下,您只是一遍又一遍地添加相同的对象。每次要将其添加到Agency
时,您都需要创建一个新的ArrayList
对象。
我不熟悉XMLParser
语法,但我想如果你改变了:
Agency agency = new Agency();
到
agency = new Agency();
中的
else if(eventType == XmlPullParser.END_TAG)
它应该修复它。