转换ForeignCollection到ArrayList中 - ORMLite,GSON和AndroidArrayList、ForeignCollection、ORMLite、Android

2023-09-04 09:59:37 作者:烟花巷陌里的那抹笑靥

我道歉,如果我不是超级清楚我的解释,但我会添加到编辑这个问题的明确性,如果要求。

我开发一个Android应用程序,它通过在本地使用ORMLite外部API和存储数据接收数据。此前在本地存储数据,并使用ORMLite我有这检索JSON从服务器模型,并通过分析它:

  GSON GSON =新GSON();

字符串结果= ApiClient.httpPost(/ user_route);

用户USER = gson.fromJson(结果,User.class);
 

User类定义

 公共类用户{
  INT ID;
  字符串名称;
  ArrayList的<图像>媒体;
}
 

和图像类:

 公共类图片{
  INT ID;
  INT creator_id;
  字符串URL;
}
 
Collection源码分析 一 ArrayList源码分析

这是模型和方法的简单重复presentation,但我相信,我已经把所有相关信息。顺便说一句,传媒是一个JSON对象,它包含了图片

现在我想也是在本地存储数据。为了让用户使用和图像之间的关系ORMLite看来你必须使用ForeignCollection类@Fo​​reignCollectionField注解。我不相信GSON可以直接解析JSON的传媒字段的用户类作为ForeignCollection对象,所以我想我需要创建两个字段 mediaCollection 传媒

现在使用ORMLite User类看起来是这样的:

  @DatabaseTable(tableName值=用户)
公共类用户{
  @DatabaseField(generatedId =真)
  INT ID;

  @DatabaseField
  字符串名称;

  @ForeignCollectionField
  ForeignCollection<图像> mediaCollection;

  ArrayList的<图像>媒体;
}
 

与ORMLite Image类看起来是这样的:

  @DatabaseTable(tableName值=图像)
公共类图片{

  @DatabaseField(generatedId =真)
  INT ID;

  @DatabaseField(外资= TRUE,foreignAutoCreate = TRUE,foreignAutoRefresh = TRUE)
  私人用户的用户;

  @DatabaseField
  INT creator_id;

  @DatabaseField
  字符串URL;

}
 

如何应用程序的作品的流动是第一I打本地数据库的用户。我执行一些逻辑,然后确定,如果我需要真正打服务器更新或刷新的用户数据。

无论数据来自本地或从远程服务器,我需要显示图片在同样的观点。目前的情况是,网址为图片存储在不同类型取决于数据是否是本地的还是远程对象。我想要做的是,如果图片保存在 ForeginCollection 对象,将其转换对象成的ArrayList ,然后继续我的code,其余其中提取图片 URL并显示出来。

我想有两个问题。

这是一个很好的计划,或者我应该写两个完全独立的解压缩图像从数据 URL,从 ForeignCollection 到的ArrayList

如果它的是的一个很好的计划,我怎么转换 ForeginCollection 的ArrayList

解决方案   

这是一个很好的计划,或者我应该写两个完全独立的数据中提取的图片网址,从ForeignCollection未进行转换的对象ArrayList中?的方式

它应该工作。我没有看到一个简单的方法,否则做到这一点。

的几点:

是否有某种方式来纪念 ForeignCollection 瞬态所以转移了JSON当它被忽略?然后,你可以使用相同的用户为JSON和ORMLite对象。 某种的水合物()的方法是好,因此它会转换传媒 - > mediaCollection ,反之亦然。 如果您需要加载JSON传输图像到国外集合,那么你应该使用Dao.getEmptyForeignCollect(...)方法。一旦你的收藏,你可以添加图片到它的时候,就会被添加到表中。   

如果这是一个很好的计划,我怎么转换ForeignCollection到一个ArrayList

这是比较容易。 A ForeignCollection 集合。你可以做一个:

 媒体=新的ArrayList<图像>(mediaCollection);
 

所以,你的 User.getMedia()方法应检查,如果是这样,从 mediaCollection 字段。

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

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

The User class was defined

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

And the Image class:

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

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.

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.

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

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.

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.

I guess there are two questions.

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?

If it is a good plan, how do I convert a ForeginCollection to an 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.

Some points:

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.

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

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

media = new ArrayList<Image>(mediaCollection);

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