Convert ForeignCollection to ArrayList - ORMLite, Gson and Android

こ雲淡風輕ζ 提交于 2019-12-02 18:00:38

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.

Just to Convert from @ForeignCollectionField to ArrayList<Object> you can do that:

ArrayList<Object> taskFieldsList = new ArrayList<Object>(your collection);

hope it's help you.

You can't create a ForeignCollection other than first storing the objects in the database, then have ormlite retrieve them in the ForeignCollection. You can use a Collection instead of a ForeignCollection, but it works the same way. What I do is the following: I have two collections in the parent class. One is an ArrayList that the json or xml is deserialized into. When the parent object gets stored, the dao stores the objects in the Arraylist to the database, in the table that's linked to the other Collection, a ForeignCollection. When the parent object is retrieved, it automatically contains the objects in the ForeignCollection. It's not perfect, but it works.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!