Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android添加ArrayList<;对象>;作为类对象中的变量_Java_Android_Class_Arraylist_Xmlpullparser - Fatal编程技术网

Java Android添加ArrayList<;对象>;作为类对象中的变量

Java Android添加ArrayList<;对象>;作为类对象中的变量,java,android,class,arraylist,xmlpullparser,Java,Android,Class,Arraylist,Xmlpullparser,我有两门课,一门是问题,另一门是选择。一些字段如下所示: 问题: public String id; public String text; public String image; public ArrayList<Choices> choices; 简单地说,我得到一些XML并对其进行解析。有问题,为了将3、4或5个答案与一个问题关联起来,我对所有选项(或答案)进行ArrayList,然后将其设置到问题类中。但我尝试读取特定对象中的列表,但它没有返回任何内容 下面是我进行最终组

我有两门课,一门是
问题
,另一门是
选择
。一些字段如下所示:

问题

public String id;
public String text;
public String image;
public ArrayList<Choices> choices;
简单地说,我得到一些XML并对其进行解析。有问题,为了将3、4或5个答案与一个问题关联起来,我对所有选项(或答案)进行
ArrayList
,然后
将其设置到问题类中。但我尝试读取特定对象中的列表,但它没有返回任何内容

下面是我进行最终组合和测试的代码:

question.setChoices(choiceList);
//Log.d("demo", ">>>" + question.getChoices());
questionList.add(question);
question = null;
choices = null;
choiceList.clear();
(较长版本)

基本上,我查看日志,可以打印
Question
对象中的所有数据,除了
ArrayList
信息。我得到的只是
[]
。我非常感谢你在这方面的帮助

编辑的额外信息

else if (parser.getName().equals("choice")) {

                boolean isCorrect;
                String answer;

                if (parser.getAttributeValue(null, "answer") != null) {
                    isCorrect = true;
                    answer = parser.nextText();
                    choices = new Choices(isCorrect, answer);
                }
                else {
                    isCorrect = false;
                    answer = parser.nextText();
                    choices = new Choices(isCorrect, answer);
                }

                choiceList.add(choices);

            } // END Choices Parsing

choicesList.clear实际上会从列表中删除所有数据。由于列表与问题对象中的列表相同,因此问题对象中的列表也会被清除。您不想清除它,而是想创建一个新的ArrayList。

我编辑了我的原始帖子。如你所见,我做的选择=新的选择()。这是一个伟大的启示,你已经,现在我正试图确定如何实现它。因为整个while循环逐行读取XML。我想我的问题是,我是否需要每次都用一个新的“名字”来创造一个新的最少值?
... // other parsing code before this

case XmlPullParser.END_TAG:

            if (parser.getName().equals("question")) {

                // Testing purposes
                //Log.d("demo", ">>>" + choiceList.toString());
                question.setChoices(choiceList);
                //Log.d("demo", ">>>" + question.getChoices());
                questionList.add(question);
                question = null;
                choices = null;
                choiceList.clear();
                Log.d("demo", ">>>" + questionList.get(0).getChoices());

            }

            break;

        default:
            break;

        } // END Switch

        event = parser.next();

    } // END While loop

    return questionList;

}

}
else if (parser.getName().equals("choice")) {

                boolean isCorrect;
                String answer;

                if (parser.getAttributeValue(null, "answer") != null) {
                    isCorrect = true;
                    answer = parser.nextText();
                    choices = new Choices(isCorrect, answer);
                }
                else {
                    isCorrect = false;
                    answer = parser.nextText();
                    choices = new Choices(isCorrect, answer);
                }

                choiceList.add(choices);

            } // END Choices Parsing