title: How can I use DataSnapshot to make an ArrayList of all the data that I have in Firebase?
body: <p>My problem is that I have a database in Firebase where I store "Day" Objects, 
  and I want to retrieve all the content of the database and put everything in an ArrayList of Days BUT when I execute the code to do it, I have an error : "Can't convert object of type java.lang.String to type com.example.android_project_test.Day"  and I have no idea on how to resolve it. </p>
  
  <p>I tried casting it, but it didn't work.</p>
  
  <p>The message is : </p>
  
  <pre><code>2019-04-20 17:52:18.158 12477-12477/com.example.android_project_test E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.example.android_project_test, PID: 12477
      com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.android_project_test.Day
          at com.google.android.gms.internal.firebase_database.zzkt.zzb(Unknown Source:209)
          at com.google.android.gms.internal.firebase_database.zzkt.zza(Unknown Source:0)
          at com.google.firebase.database.DataSnapshot.getValue(Unknown Source:10)
          at com.example.android_project_test.CalendarDbHelper.fetchData(CalendarDbHelper.java:125)
          at com.example.android_project_test.CalendarDbHelper.access$000(CalendarDbHelper.java:12)
          at com.example.android_project_test.CalendarDbHelper$1.onChildAdded(CalendarDbHelper.java:58)
          at com.google.android.gms.internal.firebase_database.zzbt.zza(Unknown Source:71)
          at com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source:2)
          at com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source:71)
          at android.os.Handler.handleCallback(Handler.java:790)
          at android.os.Handler.dispatchMessage(Handler.java:99)
          at android.os.Looper.loop(Looper.java:164)
          at android.app.ActivityThread.main(ActivityThread.java:6494)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
  </code></pre>
  
  <p>Here is my Day Class :   </p>
  
  <pre><code>public class Day {
  
      private String id;
      private String dayName;
      private String dayDate;
      private ArrayList&lt;Meal&gt; meals;
  
      Day(){}
  
      Day(String id, String dayName, String dayDate, ArrayList&lt;Meal&gt; meals) {
          this.id = id;
          this.dayName = dayName;
          this.dayDate = dayDate;
          this.meals = meals;
  
      }
  
      Day(String dayName, String dayDate, ArrayList&lt;Meal&gt; meals) {
          this.dayName = dayName;
          this.dayDate = dayDate;
          this.meals = meals;
  
      }
  
      Day(String dayName, String dayDate) {
          this.dayName = dayName;
          this.dayDate = dayDate;
  
      }
  </code></pre>
  
  <p>Here is how I add Days into the database : </p>
  
  <pre><code>public void addDay(Day day)
      {
                  String Id = db.push().getKey();
                  day.setId(Id);
                  db.child(Id).setValue(day); 
     }
  </code></pre>
  
  <p>Here is the method I use to retrieve an ArrayList from the database : </p>
  
  <pre><code>public ArrayList&lt;Day&gt; listDays()
      {
  
  
          db.addChildEventListener(new ChildEventListener() {
              @Override
              public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                  fetchData(dataSnapshot);
              }
  
              @Override
              public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                  fetchData(dataSnapshot);
  
              }
  
              @Override
              public void onChildRemoved(DataSnapshot dataSnapshot) {
  
              }
  
              @Override
              public void onChildMoved(DataSnapshot dataSnapshot, String s) {
  
              }
  
              @Override
              public void onCancelled(DatabaseError databaseError) {
  
              }
          });
  
          return listDays;
      }
  </code></pre>
  
  <p>Here is the fetchData(dataSnapshot) where I have a problem : </p>
  
  <pre><code>private void fetchData(DataSnapshot dataSnapshot)
      {
          for(DataSnapshot postSnapShot:dataSnapshot.getChildren())
          {
              Day day= postSnapShot.getValue(Day.class);
              listDays.add(day);
  
          }
      }
  </code></pre>
  
  <p>When I want to do "Day day = postSnapShot.getValue(Day.class);", what I want to do is to retrieve each child as a Day object. But what I'm getting is the message I've written above.</p>