将 ForeignCollection 转换为 ArrayList - ORMLite、Gson 和 Android [英] Convert ForeignCollection to ArrayList - ORMLite, Gson and Android

查看:21
本文介绍了将 ForeignCollection 转换为 ArrayList - ORMLite、Gson 和 Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的解释不是很清楚,我深表歉意,但如果需要,我会添加和编辑这个问题以确保清晰.

I apologize if I'm not super clear with my explanation but I'll add to and edit this question for clarity if requested.

我正在开发一个 Android 应用程序,它通过外部 API 接收数据并使用 ORMLite 在本地存储数据.在本地存储数据并使用 ORMLite 之前,我有从服务器检索 JSON 并通过以下方式解析它的模型:

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:

Gson gson = new Gson();

String result = ApiClient.httpPost("/user_route");

User user = gson.fromJson(result, User.class);

定义了 User 类

public class User {
  int id;
  String name;
  ArrayList<Image> media;
}

还有 Image 类:

And the Image class:

public class Image {
  int id;
  int creator_id;
  String url;
}

这是模型和方法的简化表示,但我相信我已经保留了所有相关信息.顺便说一句,media 是一个包含 Images 的 JSON 对象.

This is a simplified representation of the models and methods but I believe I've kept all the relevant information. BTW, media is a JSON object which contains the Images.

现在我还尝试将数据存储在本地.为了使用 ORMLite 建立用户和图像之间的关系,您似乎必须使用 ForeignCollection 类和 @ForeignCollectionField 注释.我不相信 Gson 可以直接将 User 类中 media 字段的 Json 解析为 ForeignCollection 对象,所以我认为我需要创建两个字段 mediaCollection 和 <代码>媒体.

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 media field in the User class as a ForeignCollection object, so I thought I needed to create two fields mediaCollection and media.

使用 ORMLite,User 类现在看起来像这样:

Using ORMLite the User class now looks like this:

@DatabaseTable(tableName = "Users")
public class User {
  @DatabaseField(generatedId = true)
  int id;

  @DatabaseField
  String name;

  @ForeignCollectionField
  ForeignCollection<Image> mediaCollection;

  ArrayList<Image> media;
}

带有 ORMLite 的 Image 类如下所示:

The Image class with ORMLite looks like this:

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

}

应用程序的工作流程首先是我为用户访问本地数据库.我执行一些逻辑,然后确定我是否需要实际访问服务器以更新"或刷新"用户数据.

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.

无论数据来自本地还是来自远程服务器,我都需要在同一视图中显示 Image.就目前而言,Image 的 URL 存储在不同类型的对象中,具体取决于数据是本地的还是远程的.我想要做的是,如果 Image 存储在 ForeginCollection 对象中,将该对象转换为 ArrayList 然后继续其余的我的代码提取 Image URL 并显示它.

Whether the data comes locally or from the remote server, I need to display the Image in the same view. As it stands now, the URL for the Image 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 Image is stored in a ForeginCollection object, convert that object into an ArrayList and then proceed with the rest of my code which extracts the Image URL and displays it.

我想有两个问题.

  1. 这是一个好的计划还是我应该编写两种完全独立的方法来从数据中提取 Image URL,而不是将对象从 ForeignCollection 转换为 <代码>ArrayList?

  1. Is this a good plan or should I write two completely separate ways of extracting the Image URL from the data, NOT converting the object from ForeignCollection to ArrayList?

如果一个好的计划,我如何将ForeginCollection 转换为ArrayList?

If it is a good plan, how do I convert a ForeginCollection to an ArrayList?

推荐答案

这是一个好的计划还是我应该编写两种完全独立的方法来从数据中提取图像 URL,而不是将对象从 ForeignCollection 转换为 ArrayList?

Is this a good plan or should I write two completely separate ways of extracting the Image URL from the data, NOT converting the object from ForeignCollection to ArrayList?

它应该可以工作.否则我看不出有什么简单的方法可以做到这一点.

It should work. I don't see an easy way to do it otherwise.

几点:

  • 是否有某种方法可以将 ForeignCollection 标记为瞬态,以便在通过 JSON 传输时将其忽略?然后,您可以对 JSON 和 ORMLite 使用相同的 User 对象.
  • 某种 hydrate() 方法会很好,因此它可以从 media -> mediaCollection 转换,反之亦然.
  • 如果您需要将 JSON 传输的图像加载到外部集合中,那么您应该使用 Dao.getEmptyForeignCollect(...) 方法.拥有收藏后,您可以将图像添加到其中,它们将被添加到表格中.
  • Is there some way to mark the ForeignCollection as transient so it is ignored when transferring over JSON? Then you can use the same User object for both JSON and ORMLite.
  • Some sort of hydrate() method would be good so it would convert from media -> mediaCollection and vice versa.
  • If you need to load JSON transferred images into the foreign collection then you should use the Dao.getEmptyForeignCollect(...) method. Once you have the collection, you can add images into it and they will be added to the tables.

如果这是一个好的计划,我如何将 ForeignCollection 转换为 ArrayList?

If it is a good plan, how do I convert a ForeignCollection to an ArrayList?

这更容易.ForeignCollection 是一个 Collection.你可以做一个:

This is easier. A ForeignCollection is a Collection. You can just do a:

media = new ArrayList<Image>(mediaCollection);

所以你的 User.getMedia() 方法应该检查 null,如果有,从 mediaCollection 字段转换.

So your User.getMedia() method should check for null and if so, convert from the mediaCollection field.

这篇关于将 ForeignCollection 转换为 ArrayList - ORMLite、Gson 和 Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆