0

I am expecting output to be an array list filled with all items as "thought" but instead, every time i add a value into arraylist, the previous value in list also gets update with new, hence resulting in duplicates. This behaviour goes on till end. Even though i have achieved the result by changing the approach, failure of my previous approach still bothers me. Any help or pointer will be really appreciated.

My debugging observation: When a second add() method is encountered, instead of just adding a new element to arraylist, somehow previous element gets altered too.

Below is xml snippet:

<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>

Below is a method for my trial and error:

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

}

Below is the Log info I get:
02-23 17:15:30.199: INFO/XmlPullParserActivity(726): Start document
02-23 17:15:30.299: INFO/XmlPullParserActivity(726): 4
02-23 17:15:30.299: INFO/XmlPullParserActivity(726): India
02-23 17:15:30.299: INFO/XmlPullParserActivity(726): Item Description 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): India
02-23 17:15:30.309: INFO/XmlPullParserActivity(726): Item Description 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): Item Description 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): India
02-23 17:15:30.349: INFO/XmlPullParserActivity(726): Item Description 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): Australia
02-23 17:15:30.349: INFO/XmlPullParserActivity(726): Item Description 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--------------

2
  • can you pls eleborate what this means this.cName = new Country(); and impl. of class country.? Commented Feb 23, 2011 at 7:29
  • cName is as instance of Country class. Country class contains a String variable named "name" and another variable as instance of "Thought", which itself contains several fields. Commented Feb 23, 2011 at 8:43

1 Answer 1

2

Repetition of values are because this.thought always refers to the same object.

Since, you are setting values on the same instance, you will get an ArrayList<Thought> with values, read last in parse cycle.

Sign up to request clarification or add additional context in comments.

2 Comments

"this" is implied whether you write it or not. What you have to do is instantiate a new Thought object every time you encounter <thought> node and when you get a </thought> put it in the ArrayList.
yeah, i knew it. I have already tried ur next suggestion too, but in vain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.