title: Convert ForeignCollection to ArrayList - ORMLite, Gson and Android
body: <p>I apologize if I'm not super clear with my explanation but I'll add to and edit this question for clarity if requested.</p>
  
  <p>I am developing an Android app which receives data through an external API and stores data locally using ORMLite. Prior to storing data locally and using ORMLite I had models which retrieved JSON from the server and parsed it via:</p>
  
  <pre><code>Gson gson = new Gson();
  
  String result = ApiClient.httpPost("/user_route");
  
  User user = gson.fromJson(result, User.class);
  </code></pre>
  
  <p>The User class was defined </p>
  
  <pre><code>public class User {
    int id;
    String name;
    ArrayList&lt;Image&gt; media;
  }
  </code></pre>
  
  <p>And the Image class:</p>
  
  <pre><code>public class Image {
    int id;
    int creator_id;
    String url;
  }
  </code></pre>
  
  <p>This is a simplified representation of the models and methods but I believe I've kept all the relevant information. BTW, <code>media</code> is a JSON object which contains the <code>Images</code>. </p>
  
  <p>Now I'm trying to also store the data locally. In order to have the relationship between Users and Images using ORMLite it seems you have to employ ForeignCollection class and @ForeignCollectionField annotation. I don't believe Gson can directly parse the Json for the <code>media</code> field in the User class as a ForeignCollection object, so I thought I needed to create two fields <code>mediaCollection</code> and <code>media</code>. </p>
  
  <p>Using ORMLite the User class now looks like this:</p>
  
  <pre><code>@DatabaseTable(tableName = "Users")
  public class User {
    @DatabaseField(generatedId = true)
    int id;
  
    @DatabaseField
    String name;
  
    @ForeignCollectionField
    ForeignCollection&lt;Image&gt; mediaCollection;
  
    ArrayList&lt;Image&gt; media;
  }
  </code></pre>
  
  <p>The Image class with ORMLite looks like this:</p>
  
  <pre><code>@DatabaseTable(tableName = "Images")
  public class Image {
  
    @DatabaseField(generatedId = true)
    int id;
  
    @DatabaseField(foreign=true, foreignAutoCreate=true, foreignAutoRefresh=true)
    private User user;
  
    @DatabaseField
    int creator_id;
  
    @DatabaseField
    String url;
  
  }
  </code></pre>
  
  <p>How the flow of the app works is first I hit the local database for a User. I perform some logic which then determines if I need to actually hit the server to 'update' or 'refresh' the User data.</p>
  
  <p>Whether the data comes locally or from the remote server, I need to display the <code>Image</code> in the same view. As it stands now, the URL for the <code>Image</code> is stored in different types of objects depending on whether the data is local or remote. What I would like to do is if the <code>Image</code> is stored in a <code>ForeginCollection</code> object, convert that object into an <code>ArrayList</code> and then proceed with the rest of my code which extracts the <code>Image</code> URL and displays it.</p>
  
  <p>I guess there are two questions.</p>
  
  <ol>
  <li><p>Is this a good plan or should I write two completely separate ways of extracting the <code>Image</code> URL from the data, NOT converting the object from <code>ForeignCollection</code> to <code>ArrayList</code>?</p></li>
  <li><p>If it <em>is</em> a good plan, how do I convert a <code>ForeginCollection</code> to an <code>ArrayList</code>?</p></li>
  </ol>