Convertir ForeignCollection a ArrayList - ORMLite, Gson y Android
Frecuentes
Visto 12,643 equipos
29
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
e 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
se almacena en un 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 fromForeignCollection
aArrayList
?Si is a good plan, how do I convert a
ForeginCollection
a unaArrayList
?
3 Respuestas
21
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.
Algunos puntos:
- Is there some way to mark the
ForeignCollection
as transient so it is ignored when transferring over JSON? Then you can use the sameUser
object for both JSON and ORMLite. - Alguna clase de
hydrate()
method would be good so it would convert frommedia
->mediaCollection
y viceversa. - 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
es un Collection
. You can just do a:
media = new ArrayList<Image>(mediaCollection);
Entonces tus User.getMedia()
method should check for null
and if so, convert from the mediaCollection
campo.
Respondido 03 Oct 12, 15:10
Can you expand on the conversion from a ForeignCollection
? I'm trying to obtain an array using Image[] media = mediaCollection.toArray(new Image[0])
but this throws java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase [...]
derivada de LazyForeignCollection:232
. - Pablo Lammertsma
2
Just to Convert from @ForeignCollectionField
a ArrayList<Object>
usted puede hacer eso:
ArrayList<Object> taskFieldsList = new ArrayList<Object>(your collection);
hope it's help you.
respondido 11 mar '15, 13:03
0
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.
Respondido 03 Oct 12, 00:10
This is somewhat incorrect. You can get a new ForeignCollection
mediante el uso de la Dao.getEmptyForeignCollection(...)
método. Mira aquí: ormlite.com/javadoc/ormlite-core/com/j256/ormlite/dao/… - Gray
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas android arraylist gson ormlite foreign-collection or haz tu propia pregunta.
see answer in another related thread: stackoverflow.com/a/31598771/3495371 - My House