2013-08-31 4 views
0

사용자에게 평가의 arraylist를 반환하는이 함수가 있습니다. 모든 평가에는 일련의 질문 목록이 있습니다. 데이터베이스에서 질문을 얻지 만, 사용자에게 assessment 배열을 반환 할 때 questions 배열은 비어 있습니다. 누구든지 질문 배열 채우기를 도울 수 있습니다.데이터가 데이터베이스에서 제대로 반환되지 않았습니다.

참고 : 이 나에게 크기의 예를 제공 우는 내가 코드에서이 Log.v("--", "Questions: " + questions.size());.

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; 
    } 

Assessmen 클래스 :

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; 
    } 

} 

[질문 클래스 :

90,하지만 그 이후 행이, 내가 Log.v("--", "AssessmentQuestions: "+assessment.getQuestions().size());을 할 때 나에게 여기

size: 0를 제공 내 코드입니다

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; 
    } 
} 
+1

Assesment 생성자는 어떻게 생겼습니까? 물론 모든 것이 괜찮습니까? – pumpkee

+0

실은 Assessment에 'new'가있는 항목이 있습니까? 문제가 될 것이라고 생각합니다. 관계없이 우리는 완전한 (짧은) 프로그램이 필요합니다 –

+0

@RichardTingle 당신이 대답에서 나를 더 설명 할 수 있습니까? –

답변

2

생성자 내에서 질문 배열 목록에 대한 참조를 IED, 아래의 코드에 표시된 지점을 참조하십시오 새로운 ArrayList를 만들었습니다 : 당신은 내가 볼 수까지, 라인 questions = new ArrayList<Question>();와 함께 할 려

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 

} 

단순히 생략 될 수 있습니다.

+0

하하는이 코드를 잊어 버렸습니다. 도움에 감사드립니다! –

+0

@DarkoPetkovski 예전의 디버그 코드에 의해 트립 된 횟수. 다행스럽게 도울 수있어! –

관련 문제