简体   繁体   中英

Data not returned properly from database

I have this function that returns arraylist of assessments to the user. Every assessment has a arrayList of questions. I get the questions from the database but, when I'm returning the assessment arrayList to the user the questions arrayList is empty. Can anyone help me with populating the questions array.

Note: When I do this Log.v("--", "Questions: " + questions.size()); from the code bellow it gives me the size ex. 90, but a row after that, when I do Log.v("--", "AssessmentQuestions: "+assessment.getQuestions().size()); it gives me size: 0

Here is my code:

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

The Assessmen class:

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

}

The Question class:

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

Within your constructor you haven't copied the reference to the questions array list, you've created a new ArrayList, see the marked point in the code below:

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

}

What are you trying to do with the line questions = new ArrayList<Question>(); , as far as I can see it can simply be omitted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM