从自定义对象的ArrayList访问方法

时间:2013-09-16 16:10:25

标签: java android arraylist

我遇到了问题,我认为这是一个概念错误

我创建了一个java类,当调用构造函数时,会创建另一个自定义对象

第一次使用param start == true和

运行代码
questions  = new ArrayList<Question>();
            questions.add(this.question);

然后调用writeXML并正常工作

但是如果start!= true则首先读取xml的内容并存储在自定义问题对象的arraylist中

questions = readXML(ctx);
questions.add(this.question);
readXML return a arraylist of questions objects from readed xml

问题是当代码到达

时调用writexml方法发送arraylist作为参数
 for( Question q : questions ){


                serializer.startTag(null, "question");
                serializer.attribute(null,"grupo" , q.getGroup());
                serializer.attribute(null,"question" , q.getQuestion());
                serializer.text(q.getAnswer());
                serializer.endTag(null, "question");
            }

当代码尝试执行serializer.attribute(null,“grupo”,q.getGroup()); q.getGroup()失败并转到捕获带有空消息的异常

我可以做错什么?

非常感谢

这是完整的课程

package...
inports...

class AsyncLoadXML extends AsyncTask<Boolean, Integer, String> {

    private Context ctx;    
    private List<Question> questions;
    private Question question;
    String TAG;

    public AsyncLoadXML(Context c, String g, String p, String r) {
        this.ctx = c;
        question =new Question();
        question.setGroup(g);
        question.setQuestion(p);
        question.setAnswer(r);

    }

    @Override
    protected String doInBackground(Boolean... start) {

        if(!start[0]){
            try { questions = readXML(ctx);
            } 
            catch (IOException e) { e.printStackTrace(); }
        }

        else {questions  = new ArrayList<Question>();
        questions.add(this.question);
        }

        try { writeXML(ctx, questions); } 
        catch (IOException e) { e.printStackTrace(); }


        return "done";
    }
    protected void onPostExecute(String result) {
        if(!result.equals("")) 
            Toast.makeText(this.ctx, result, Toast.LENGTH_LONG).show();
    }



    public String writeXML(Context ctx, List<Question> questions) throws IOException{   
        FileOutputStream fout = null;

        try {
            fout = ctx.openFileOutput("answers.xml", Context.MODE_PRIVATE);
        } catch (FileNotFoundException e) {
            return e.getMessage();

        }

        XmlSerializer serializer = Xml.newSerializer();
        try {
            serializer.setOutput(fout, "UTF-8");
            serializer.startDocument(null, true);
            serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);

            serializer.startTag(null, "questions");


            for( Question q : questions ){


                serializer.startTag(null, "question");
                serializer.attribute(null,"grupo" , q.getGroup());
                serializer.attribute(null,"question" , q.getQuestion());
                serializer.text(q.getAnswer());
                serializer.endTag(null, "question");
            }

            serializer.endTag(null, "questions");


            serializer.endDocument();
            serializer.flush();
            fout.close();
            return "done";

        } catch (Exception e) {
            return e.getMessage();

        }
    }





    public List<Question> readXML(Context ctx) throws IOException{  
        FileInputStream fin = null;
        questions= new ArrayList<Question>();
        try {
            fin = ctx.openFileInput("answers.xml");
        } catch (Exception e) {

        }

        XmlPullParser parser = Xml.newPullParser();
        try {
            parser.setInput(fin, "UTF-8");

            int event = parser.next();
            while(event != XmlPullParser.END_DOCUMENT) {

                if(event == XmlPullParser.START_TAG) {


                    question = new Question();

                    for(int i = 0; i < parser.getAttributeCount(); i++) {

                        if(parser.getAttributeName(i) == "group") question.setGroup(parser.getAttributeValue(i));
                        else if(parser.getAttributeName(i) == "question") question.setQuestion(parser.getAttributeValue(i));

                    }

                }

                if(event == XmlPullParser.TEXT && parser.getText().trim().length() != 0)
                {question.setAnswer(parser.getText());

                questions.add(question);
                }


                if((event == XmlPullParser.END_TAG))
                {
                    Log.d(TAG, "</"+ parser.getName() + ">");}


                event = parser.next();
            }
            fin.close();


        } catch (Exception e) {

            e.printStackTrace();
        }
        return questions;
    }

}

这是一个对象类

public class Question {

    private String group;
    private String question;
    private String answer;


    public Question(){

    }
    public Question(String g, String p, String r){
    group = g;
    question = p;
    answer = r;
    }

    public String getGroup(){
        return group;
    }

    public String getQuestion(){
        return question;
    }

    public String getAnswer(){
        return answer;
    }

    public void setGroup(String g){
        group = g;
    }

    public void setQuestion(String p){
        question = p;
    }

    public void setAnswer(String r){
        answer = r;
    }
}

0 个答案:

没有答案