I am trying to build an ArrayList of CourseCardModel objects to feed into an adapter to generate a Swipeable Cards style view.
The data is being pulled from a Firebase realtime database, and being compiled in the OnDataChange() listener within a generateCourseCards() method.
For each snapshot I cast the Object as a custom Course object, print some information to the console to check I can access a member variable of Course, attach the Course object to a CardModel object, and then add that CourseModel object to a CourseCardModelList.
As the loop works through the data snapshots, I can see that it is correctly casting the object, and adding it to the list. However at the end of the loop, I reiterate through the CoursecardModelList and print out the CourseName variable, and it appears that every Course in the list has been set to the LAST Course that was added to the list.
Any help with this issue would be hugely appreciates, i'm not sure what is happening to the ArrayList after the loop and before the end of the OnDataChange() method that is causing it to overwrite all its entries to just the last course added.
generateCourses method
private ArrayList<CourseCardModel> generateCourseCards() {
courseCardModelList = new ArrayList<CourseCardModel>();
cardModel = new CourseCardModel();
dbref = FirebaseDatabase.getInstance().getReference().child("courses");
// Retrieve the course data from firebase db and cast as Course object
dbref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
Log.e("Count " ,"" + snapshot.getChildrenCount());
int i = 0;
for (DataSnapshot postSnapshot: snapshot.getChildren()) {
c = postSnapshot.getValue(Course.class);
System.out.println("COURSE INFO: " + c.getCourseName());
cardModel.setCourse(c);
System.out.println("COURSE INFO FROM CARDMODEL: " + cardModel.getCourse().getCourseName());
courseCardModelList.add(cardModel);
System.out.println("COURSE INFO FROM COURSECARDMODELLIST: " + courseCardModelList.get(i).getCourse().getCourseName());
System.out.println("COURSE CARD MODEL LIST SIZE: " + courseCardModelList.size());
i++;
}
ca.notifyDataSetChanged();
for (int k = 0; k < courseCardModelList.size(); k++) {
System.out.println(k + " COURSE INFO LOOP AT END: " + courseCardModelList.get(k).getCourse().getCourseName());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e("The read failed: ", databaseError.getMessage());
}
});
System.out.print("END OF METHOD ARRAY SIZE CHECK: " + courseCardModelList.size());
datapulled = true;
return courseCardModelList;
}
snippet of logcat output where the first loop finishes and the second reiterates through:
01-22 13:13:25.640 18800-18800/coursematch.coursematchuk I/System.out: COURSE INFO: Adult Nursing BN (Hons)
01-22 13:13:25.640 18800-18800/coursematch.coursematchuk I/System.out: COURSE INFO FROM CARDMODEL: Adult Nursing BN (Hons)
01-22 13:13:25.640 18800-18800/coursematch.coursematchuk I/System.out: COURSE INFO FROM COURSECARDMODELLIST: Adult Nursing BN (Hons)
01-22 13:13:25.640 18800-18800/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 1686
01-22 13:13:25.640 18800-18800/coursematch.coursematchuk I/System.out: COURSE INFO: Conservation with Forestry BSc (Hons)
01-22 13:13:25.640 18800-18800/coursematch.coursematchuk I/System.out: COURSE INFO FROM CARDMODEL: Conservation with Forestry BSc (Hons)
01-22 13:13:25.640 18800-18800/coursematch.coursematchuk I/System.out: COURSE INFO FROM COURSECARDMODELLIST: Conservation with Forestry BSc (Hons)
01-22 13:13:25.640 18800-18800/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 1687
01-22 13:13:25.640 18800-18800/coursematch.coursematchuk I/System.out: COURSE INFO: Mental Health Nursing BN (Hons)
01-22 13:13:25.640 18800-18800/coursematch.coursematchuk I/System.out: COURSE INFO FROM CARDMODEL: Mental Health Nursing BN (Hons)
01-22 13:13:25.640 18800-18800/coursematch.coursematchuk I/System.out: COURSE INFO FROM COURSECARDMODELLIST: Mental Health Nursing BN (Hons)
01-22 13:13:25.640 18800-18800/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 1688
01-22 13:13:25.640 18800-18800/coursematch.coursematchuk I/System.out: 0 COURSE INFO LOOP AT END: Mental Health Nursing BN (Hons)
01-22 13:13:25.640 18800-18800/coursematch.coursematchuk I/System.out: 1 COURSE INFO LOOP AT END: Mental Health Nursing BN (Hons)
01-22 13:13:25.640 18800-18800/coursematch.coursematchuk I/System.out: 2 COURSE INFO LOOP AT END: Mental Health Nursing BN (Hons)
01-22 13:13:25.640 18800-18800/coursematch.coursematchuk I/System.out: 3 COURSE INFO LOOP AT END: Mental Health Nursing BN (Hons)
01-22 13:13:25.640 18800-18800/coursematch.coursematchuk I/System.out: 4 COURSE INFO LOOP AT END: Mental Health Nursing BN (Hons)
01-22 13:13:25.640 18800-18800/coursematch.coursematchuk I/System.out: 5 COURSE INFO LOOP AT END: Mental Health Nursing BN (Hons)
01-22 13:13:25.640 18800-18800/coursematch.coursematchuk I/System.out: 6 COURSE INFO LOOP AT END: Mental Health Nursing BN (Hons)