gpt4 book ai didi

java - 数据未从数据库正确返回

转载 作者:行者123 更新时间:2023-12-02 06:46:48 24 4
gpt4 key购买 nike

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

注意:当我从下面的代码中执行此 Log.v("--", "Questions: "+ questions.size()); 时,它会给出大小 ex。 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;
}
}

最佳答案

在构造函数中,您尚未复制对问题数组列表的引用,而是创建了一个新的 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>(); 行做什么? ,据我所知,可以简单地省略它。

关于java - 数据未从数据库正确返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18551470/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com