3

how to create table for a custom object that contain a list of like this

public class Mobel implements Parcelable {
    int thumbnail;
    List<Integer> pics;
    int price;
    String joziat;
    String code;
}

as you know there is no problem with int and string columns but how should i store my list of integer ? i was thinking to make a separate table for it but as i don't know ho many pic i have for particular object have no idea how should i join them

1 Answer 1

10

While indeed you can not store objects in sqlite, you can serialize it and store the serialized object:

Gson gson = new Gson();
String toStoreObject = gson.toJson(modelObject, Model.class);

And when you get the string back just do:

Model modelObject = gson.fromJson(storedObjectString, Model.class);

Or just serialize the property of the object (in your case the List).

Different things I would do:

  1. Create another table for the images and link that table to the table in which you store your Model object (n to 1 relationship)

  2. Don't store images in sqlite. Store them in the internal storage and in sqlite save just the path to the file.

  3. If you really really want to store the images in sqlite, convert the image to a base64 string and store that one.

Sign up to request clarification or add additional context in comments.

3 Comments

ty man ! but how can i convert a list of integer to json and later convert that json to a list of integer ?
To get the serialized list as a String: String serializedList = gson.toJson(pics); To get the list from a serialized string: Type type = new TypeToken<List<Integer>>() {}.getType(); List<Integer> pics = gson.fromJson(serializedList, type);
again thanks man . your anser really helped me , sry i cant give you +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.