我希望输出是一个数组列表,其中包含所有项目“思想” 但相反,每次我在arraylist中添加一个值时,list中的前一个值也会使用new进行更新,从而导致重复。这种行为一直持续到最后。即使我通过改变方法取得了成果,我之前的方法的失败仍然困扰着我。任何帮助或指针都将非常感激。
我的调试观察:当遇到第二个add()方法时,不仅仅是向arraylist添加一个新元素,以前的元素也会被改变。
以下是xml片段:
<Thoughts>
<country>
<name>India</name>
<item>
<itemId>1</itemId>
<itemDetailImage>detail.png</itemDetailImage>
<itemImage>image1.png</itemImage>
<itemDesc>Item Description 1</itemDesc>
<itemTitle>Item Title 1</itemTitle>
</item>
<item>
<itemId>2</itemId>
<itemDetailImage>detail2.png</itemDetailImage>
<itemImage>image2.png</itemImage>
<itemDesc>Item Description 2</itemDesc>
<itemTitle>Item Title 2</itemTitle>
</item>
<item>
<itemId>3</itemId>
<itemDetailImage>detail3.png</itemDetailImage>
<itemImage>image3.png</itemImage>
<itemDesc>Item Description 3</itemDesc>
<itemTitle>Item Title 3</itemTitle>
</item>
以下是我的试错方法:
private void printAll() throws XmlPullParserException, IOException{
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser parser = factory.newPullParser();
URL url = new URL("http://192.168.1.3/DefaultXML.xml");
URLConnection ucon = url.openConnection();
InputStream is = ucon.getInputStream();
parser.setInput(is, null);
int eventType = parser.getEventType();
String name;
while(eventType != parser.END_DOCUMENT){
if(eventType == parser.START_DOCUMENT){
Log.i(TAG,"Start document");
}else if (eventType == parser.START_TAG ) {
name = parser.getName();
if(name.equalsIgnoreCase(NAME)){
this.cName = new Country();
this.cName.setName(parser.nextText());
}else if(name.equalsIgnoreCase(ITEM_DESC)){
this.thought.setItemDesc(parser.nextText());
}else if (name.equalsIgnoreCase(ITEM_ID)) {
this.thought.setItemID(parser.nextText());
}else if (name.equalsIgnoreCase(ITEM_DETAIL_IMAGE)) {
this.thought.setItemDetailImage(parser.nextText());
}else if (name.equalsIgnoreCase(ITEM_IMAGE)) {
this.thought.setItemImage(parser.nextText());
}else if (name.equalsIgnoreCase(ITEM_TITLE)) {
this.thought.setItemTitle(parser.nextText());
}
}else if (eventType == parser.END_TAG) {
name = parser.getName();
if(name.equalsIgnoreCase(ITEM)) {
this.cName.setThought(this.thought);
ctry.add(cName);
this.thought = new Thought();
}
}
eventType = parser.next();
}
//Log.i(TAG,"---" + ctry.toString());
}
以下是我收到的日志信息:
02-23 17:15:30.199:INFO / XmlPullParserActivity(726):启动文档
02-23 17:15:30.299:INFO / XmlPullParserActivity(726):4
02-23 17:15:30.299:INFO / XmlPullParserActivity(726):印度
02-23 17:15:30.299:INFO / XmlPullParserActivity(726):项目描述4
02-23 17:15:30.299:INFO / XmlPullParserActivity(726):detail4.png
02-23 17:15:30.309:INFO / XmlPullParserActivity(726):image4.png
02-23 17:15:30.309:INFO / XmlPullParserActivity(726):Item Title 4 --------------
02-23 17:15:30.309:INFO / XmlPullParserActivity(726):4
02-23 17:15:30.309:INFO / XmlPullParserActivity(726):印度
02-23 17:15:30.309:INFO / XmlPullParserActivity(726):项目描述4
02-23 17:15:30.319:INFO / XmlPullParserActivity(726):detail4.png
02-23 17:15:30.319:INFO / XmlPullParserActivity(726):image4.png
02-23 17:15:30.319:INFO / XmlPullParserActivity(726):Item Title 4 --------------
02-23 17:15:30.319:INFO / XmlPullParserActivity(726):4
02-23 17:15:30.319:INFO / XmlPullParserActivity(726):印度
02-23 17:15:30.329:INFO / XmlPullParserActivity(726):项目描述4
02-23 17:15:30.329:INFO / XmlPullParserActivity(726):detail4.png
02-23 17:15:30.329:INFO / XmlPullParserActivity(726):image4.png
02-23 17:15:30.339:INFO / XmlPullParserActivity(726):Item Title 4 --------------
02-23 17:15:30.339:INFO / XmlPullParserActivity(726):4
02-23 17:15:30.339:INFO / XmlPullParserActivity(726):印度
02-23 17:15:30.349:INFO / XmlPullParserActivity(726):项目描述4
02-23 17:15:30.349:INFO / XmlPullParserActivity(726):detail4.png
02-23 17:15:30.349:INFO / XmlPullParserActivity(726):image4.png
02-23 17:15:30.349:INFO / XmlPullParserActivity(726):Item Title 4 --------------
02-23 17:15:30.349:INFO / XmlPullParserActivity(726):4
02-23 17:15:30.349:INFO / XmlPullParserActivity(726):澳大利亚
02-23 17:15:30.349:INFO / XmlPullParserActivity(726):项目描述4
02-23 17:15:30.349:INFO / XmlPullParserActivity(726):detail4.png
02-23 17:15:30.349:INFO / XmlPullParserActivity(726):image4.png
02-23 17:15:30.349:INFO / XmlPullParserActivity(726):Item Title 4 --------------
答案 0 :(得分:2)
重复值是因为this.thought
始终引用同一个对象。
因为,您在同一个实例上设置值,您将得到一个带有值的ArrayList<Thought>
,在解析周期中读取最后一个。