我有这个功能,可以向用户返回评估的arraylist。每个评估都有一个问题列表。我从数据库中得到问题但是,当我将评估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;
}
}
答案 0 :(得分: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
}
你想用questions = new ArrayList<Question>();
行做什么,据我所知,它可以简单地省略。