Convert ForeignCollection to ArrayList - ORMLite, Gson and Android

后端 未结 3 622
悲哀的现实
悲哀的现实 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<Image>(mediaCollection);
    

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

    0 讨论(0)
  • 2021-02-01 04:26

    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.

    0 讨论(0)
  • 2021-02-01 04:27

    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.

    0 讨论(0)
提交回复
热议问题