0

我有这个函数可以将评估的数组列表返回给用户。每个评估都有一个问题数组列表。我从数据库中获取问题,但是,当我将评估 arrayList 返回给用户时,问题 arrayList 为空。谁能帮我填充问题数组。

注意: 当我Log.v("--", "Questions: " + questions.size());从下面的代码中执行此操作时,它给了我前的大小。90,但在那之后的一排,当我这样做时, Log.v("--", "AssessmentQuestions: "+assessment.getQuestions().size());它给了我size: 0

这是我的代码:

public ArrayList<Assessment> getAllAsssessments() {
        ArrayList<Assessment> assessments = new ArrayList<Assessment>();
        Assessment assessment;
        String selectQuery = "SELECT * FROM " + TSCYC;
        Cursor cursor = database.rawQuery(selectQuery, null);
        if (cursor.moveToFirst()) {
            do {
                int AssessmentID = cursor.getInt(0);
                int childID = cursor.getInt(1);
                int doctorID = cursor.getInt(2);
                String assessmentDate = cursor.getString(3);
                Log.v("--", assessmentDate);
                ArrayList<Question> questions = new ArrayList<Question>();
                Question q;
                assessment = new Assessment(AssessmentID, assessmentDate,
                        doctorID, childID, questions);
                for (int i = 0; i < 90; i++) {
                    q = new Question("i", (i + 1));
                    q.setAnswer(cursor.getInt(i + 4));
                    questions.add(q);
                }
                Log.v("--", "Questions: " + questions.size());
                assessment = new Assessment(AssessmentID, assessmentDate,
                        doctorID, childID, questions);
                assessments.add(assessment);
                Log.v("--", "AssessmentQuestions: "
                        + assessment.getQuestions().size());
            } while (cursor.moveToNext());

        }
        return assessments;
    }

评估员类:

import java.io.Serializable;
import java.util.ArrayList;

public class Assessment implements Serializable {

    private static final long serialVersionUID = 1288566422025895376L;
    private String date;
    private int doctorID, childID, id;
    private ArrayList<Question> questions;

    public Assessment(int id, String date, int doctorID, int childID,
            ArrayList<Question> questions) {
        questions = new ArrayList<Question>();
        this.id = id;
        this.setDate(date);
        this.setDoctorID(doctorID);
        this.setChildID(childID);
        this.setQuestions(questions);

    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public int getDoctorID() {
        return doctorID;
    }

    public void setDoctorID(int doctorID) {
        this.doctorID = doctorID;
    }

    public int getChildID() {
        return childID;
    }

    public void setChildID(int childID) {
        this.childID = childID;
    }

    public ArrayList<Question> getQuestions() {
        return questions;
    }

    public void setQuestions(ArrayList<Question> questions) {
        this.questions = questions;
    }

}

问题类:

public class Question implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 8703113563916355470L;
    private String question;
    private int answer, position;

    public Question(String question, int position) {
        answer = -1;
        this.question = question;
        this.position = position;
    }

    public int getAnswer() {
        return answer;
    }

    public void setAnswer(int answer) {
        this.answer = answer;
    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    public String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }
}
4

1 回答 1

2

在您的构造函数中,您没有复制对问题数组列表的引用,您已经创建了一个新的 ArrayList,请参见下面代码中的标记点:

public Assessment(int id, String date, int doctorID, int childID,
        ArrayList<Question> questions) {
    questions = new ArrayList<Question>(); //<-- here you make a NEW empty ArrayList
    this.id = id;
    this.setDate(date);
    this.setDoctorID(doctorID);
    this.setChildID(childID);
    this.setQuestions(questions); //<-- here you setQuestions as the empty arraylist

}

你想用 line 做什么questions = new ArrayList<Question>();,据我所知,它可以简单地省略。

于 2013-08-31T18:46:36.700 回答