创建的Andr​​oid ArrayList的错误 [英] Creating Android ArrayList Error

查看:135
本文介绍了创建的Andr​​oid ArrayList的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从我的XML文件解析的数据,并把它变成一个数组,然后接着将其输出到另一个函数。

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

这告诉我,

在ArrayList类型的方法Add(字符串)不适用于参数(AnimalList.Animal)

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.

推荐答案

编译器告诉你,你正在试图将错误类型的对象添加到列表中。您已经定义的列表,包含字符串,但你正在尝试添加对象的类型是动物。

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

这篇关于创建的Andr​​oid ArrayList的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆