title: retrieving data from firebase database and storing in an array list
body: <p>I am attempting to create a small course aggregator program in the form of an android application.</p>
  
  <p>My courses are all stored in a Firebase Realtime Database, which is viewable from the firebase console and everything appears fine.</p>
  
  <p>The issue is that I have written a java method to connect to the DB, retrieve the data from the DB, cast the data as a Custom Java object <code>Course</code>, attach it to another custom Java object <code>CourseCardModel</code>, and then save the <code>CourseCardModel</code> object into an ArrayList.</p>
  
  <p>The connection to the database is made successfully, and the snapshot it generates contains the correct information, which I have verified by looping through the snapshot and printing a variable <code>Course Name</code> of each <code>Course</code> object out successfully. The problem is that when the method completes, for some reason the arrayList is returns is empty, even though upon checking the size of the ArrayList throughout the snapshot iteration I can see the <code>CardModel</code> objects being added to it.</p>
  
  <p>If anybody has any help in solving this it would be hugely appreciated, I am fairly new to Firebase.</p>
  
  <p><strong>GenerateCourses() Method</strong></p>
  
  <pre><code>private ArrayList&lt;CourseCardModel&gt; generateCourseCards() {
  
      courseCardModelList = new ArrayList&lt;CourseCardModel&gt;();
      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());
              for (DataSnapshot postSnapshot: snapshot.getChildren()) {
  
                  c = postSnapshot.getValue(Course.class);
                  System.out.println("COURSE INFO: " + c.getCourseName());
  
                  cardModel.setCourse(c);
  
                  courseCardModelList.add(cardModel);
  
                  System.out.println("COURSE CARD MODEL LIST SIZE: " + courseCardModelList.size());
  
              }
          }
          @Override
          public void onCancelled(DatabaseError databaseError) {
              Log.e("The read failed: ", databaseError.getMessage());
          }
  
      });
  
      System.out.print("END OF METHOD ARRAY SIZE CHECK: " + courseCardModelList.size());
  
      return courseCardModelList;
  
  }
  </code></pre>
  
  <p><strong>LOGCAT Output</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>01-11 09:57:57.105 28709-28780/coursematch.coursematchuk D/FA: Connected to remote service
  01-11 09:57:57.105 28709-28780/coursematch.coursematchuk V/FA: Processing queued up service tasks: 4
  01-11 09:57:59.625 28709-28709/coursematch.coursematchuk E/Count: 200
  01-11 09:57:59.650 28709-28709/coursematch.coursematchuk I/System.out: END OF METHOD ARRAY SIZE CHECK: 0COURSE INFO: Physiotherapy BSc (Hons)
  01-11 09:57:59.650 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 1
  01-11 09:57:59.650 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: History and Archaeology BA (Hons)
  01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 2
  01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Arabic and French MA (Hons)
  01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 3
  01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Social Policy and Spanish BA (Hons)
  01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 4
  01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Modern Languages (French and Spanish) and Greek (with year abroad) MA (Hons)
  01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 5
  01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Politics BA (Hons)
  01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 6
  01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Nursing (Child) BSc (Hons)
  01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 7
  01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Electrical and Electronic Engineering MEng (Hons)
  01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 8
  01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: English and Russian MA (Hons)
  01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 9
  01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: French and Medieval History MA (Hons)
  01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 10
  01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: French and Modern History MA (Hons)
  01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 11
  01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Physics with Sports Science BSc (Hons)
  01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 12
  01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Electronic Engineering with Management BEng (Hons)
  01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 13
  01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Optometry BSc (Hons)
  01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 14
  01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Social Policy and Sociology BA (Hons)
  01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 15
  01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Comparative Literature and French and Russian (with year abroad) MA (Hons)
  01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 16
  01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Statistics Economics and Finance BSc (Hons)
  01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 17
  01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Mechanical Engineering BEng (Hons)
  01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 18
  01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: International Business Management (Spain) BBA (Hons)
  01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 19
  01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Ancient History and Archaeology BA (Hons)
  01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 20
  01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Electronic Engineering MEng (Hons)
  01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 21
  01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Drama and Music BA (Hons)
  01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 22
  01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Chemistry BSc (Hons)
  01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 23
  01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Medicine MB
  01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 24
  01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Linguistics MA (Hons)
  01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 25
  01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Aeronautical Engineering BEng (Hons)
  01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 26
  01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: International Business and Marketing BSc (Hons)
  01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 27
  01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: German and Theatre and Performance BA (Hons)
  01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 28
  01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Social Anthropology BA (Hons)
  01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 29
  01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Social Policy and Spanish BA (Hons)
  01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 30
  01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Modern Languages (French and Spanish) and Greek MA (Hons)
  01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 31
  01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Accounting and Mathematics and Statistics BA (Hons)
  01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 32
  01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Art History and French MA (Hons)
  01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 33
  01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Accounting and Business Analysis and Technology BA (Hons)
  01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 34
  01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Modern Languages (Russian and Spanish) and English (with integrated year abroad) MA (Hons)
  01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 35
  01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Social Policy and Crime BA (Hons)
  01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 36
  01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: International Business and Marketing BSc (Hons)
  01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 37
  01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Statistics Economics and a Language BSc (Hons)
  01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 38
  01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Social Work BA (Hons)
  01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 39
  01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Combined Honours in Social Sciences BA (Hons)
  01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 40
  01-11 09:57:59.680 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Comparative Literature and Russian and Spanish (with year abroad) MA (Hons)
  01-11 09:57:59.680 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 41
  01-11 09:57:59.680 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Automotive Engineering BEng (Hons)
  01-11 09:57:59.680 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 42
  01-11 09:57:59.680 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Electrical Engineering MEng (Hons)
  01-11 09:57:59.680 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 43
  01-11 09:57:59.680 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Comparative Literature and Russian and Spanish MA (Hons)
  01-11 09:57:59.680 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 44
  01-11 09:57:59.685 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Economics and Finance (with European study) BA (Hons)
  01-11 09:57:59.685 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 45
  01-11 09:57:59.685 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Mathematics Statistics and Finance BSc (Hons)
  01-11 09:57:59.685 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 46
  01-11 09:57:59.685 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: French and German Studies BA (Hons)
  01-11 09:57:59.685 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 47
  01-11 09:57:59.685 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Sociology BA (Hons)
  01-11 09:57:59.685 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 48
  01-11 09:57:59.685 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Electronic Engineering with Management MEng (Hons)
  01-11 09:57:59.685 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 49
  01-11 09:57:59.690 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Mechanical Engineering BEng (Hons)
  01-11 09:57:59.690 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 50
  01-11 09:57:59.690 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Medicine (graduate entry) BMBS
  01-11 09:57:59.690 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 51
  01-11 09:57:59.690 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Electrical Engineering BEng (Hons)
  01-11 09:57:59.690 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 52
  01-11 09:57:59.690 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Automotive Engineering MEng (Hons)
  01-11 09:57:59.690 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 53</code></pre>
  </div>
  </div>
  </p>