我期待的輸出是一個數組列表,其中包含所有項目「思考」 ,但相反,每次我將值添加到arraylist時,列表中的前一個值也會用new更新,因此會導致重複。這種行爲持續到最後。儘管我通過改變方法取得了成果,但我以前的方法失敗仍然困擾着我。任何幫助或指針將非常感激。xmlpullparser(),我的代碼有什麼問題?
我調試觀察:當遇到第二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標題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):India
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 ------ --------
你可以說明這是什麼意思this.c.c =新國家();和impl。的階級國家。? – 2011-02-23 07:29:02
cName是Country類的實例。 Country類包含一個名爲「name」的String變量,另一個變量爲「Thought」的實例,它本身包含多個字段。 – Avinazz 2011-02-23 08:43:13