Convert ForeignCollection to ArrayList - ORMLite, Gson and Android

后端 未结 3 621
悲哀的现实
悲哀的现实 2021-02-01 04:06

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 d

3条回答
  •  鱼传尺愫
    2021-02-01 04:14

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

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

提交回复
热议问题