0

I am parsing the data from my XML file and putting it into an array, which then goes on to output it in another function.

private void parseXML(XmlPullParser parser) throws XmlPullParserException,IOException
{
    ArrayList<String> animals = new ArrayList<String>();


    int eventType = parser.getEventType();
    Animal currentAnimal = null;


    while (eventType != XmlPullParser.END_DOCUMENT)
    {

        String name = null;
        switch (eventType)
        {
            case XmlPullParser.START_DOCUMENT:
                //animals = new ArrayList();
                break;
            case XmlPullParser.START_TAG:
                name = parser.getName();
                if (name.equalsIgnoreCase( "animal"))
                {
                    currentAnimal = new Animal();
                } 
                else if (currentAnimal != null)
                {
                    if (name.equalsIgnoreCase( "specificLocation"))
                    {
                        currentAnimal.specificLocation = parser.nextText();
                    } 
                    else if (name .equalsIgnoreCase( "name")){
                        currentAnimal.name = parser.nextText();
                    } 
                    else if (name .equalsIgnoreCase( "location"))
                    {
                        currentAnimal.location= parser.nextText();
                    }  
                    else if (name .equalsIgnoreCase( "image"))
                    {
                        currentAnimal.image= parser.nextText();
                    }  
                    else if (name .equalsIgnoreCase(  "animalInfo"))
                    {
                        currentAnimal.animalInfo= parser.nextText();
                    }  
                    else if (name .equalsIgnoreCase(  "todaysFeed"))
                    {
                        currentAnimal.todaysFeed= parser.nextText();
                    }  
                }
                break;
            case XmlPullParser.END_TAG:
                name = parser.getName();
                if (name.equalsIgnoreCase("animal") && currentAnimal != null){
                    animals.add(currentAnimal);
                } 
        }
        eventType = parser.next();
    }

    printAnimals(animals);
}  

I'm getting an error at the line

animals.add(currentAnimal);

which tells me

The method add(String) in the type ArrayList is not applicable for the arguments (AnimalList.Animal)

I originally had Animal instead of string, but I changed it as I couldn't output it, and I wasn't sure if that was why.

1 Answer 1

1

The compiler is telling you that you are trying to add the wrong type of object to the list. You have defined the list as containing Strings, yet the object you are attempting to add is of type Animal.

You should define your list as containing Animal objects:

ArrayList<Animal> animals = new ArrayList<Animal>();
Sign up to request clarification or add additional context in comments.

4 Comments

That's what I originally had but then
private void printAnimals(ArrayList<Animal> animals) { Animal[] animalArray = new Animal[animals.size()]; animalArray = animals.toArray(animalArray); ListView listView = (ListView) findViewById(R.id.listViewAnimals); ArrayAdapter<Animal> animalAdapter = new ArrayAdapter<Animal>(this, android.R.layout.simple_list_item_1, animals); listView.setAdapter(animalAdapter); } wouldn't work
Plus it is not clear what your current problem is. Are you trying to fix a compilation error, or is something else wrong?
Actually, I understand, I thought I had to use String rather than my own defined, but if I can use animal then your solution works for me

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.