1
votes

In my application I create an ArrayList that is holding my calendar event objects (WeekViewEvents) with different fields (mostly Strings and int's):

ArrayList<WeekViewEvent> mNewEvents = new ArrayList<>;
...
WeekViewEvent event = new WeekViewEvent();
event.setName("abc");
event.setId(123);
mNewEvents.add(event);

And that Array (in its form) is directly used by Calendar Library Now, I want to save and read the list in internal memory using Room Persistence Library. The thing is that I'm not sure:

  1. if I will be able to load my ArrayList and use it the same way I do now even if it needs @TypeConverter?

  2. can I write my @Entity class like below: ?

    @Entity public class Events {

    @NonNull @PrimaryKey public ArrayList mNewEventsRPL;

    public ArrayList getEvents() { return mNewEventsRPL; }

    public void setEvents(ArrayList evenciki) { this.mNewEventsRPL=evenciki; }}

  3. I've converted single event object to String using GSON and it looks like this:

    {"mAllDay":false,"mColor":-6454059,"mEndTime":{"year":2018,"month":0,"dayOfMonth":30,"hourOfDay":16,"minute":46,"second":35},"mId":"111222333","mLocation":"klient","mName":"usluga","mStartTime":{"year":2018,"month":0,"dayOfMonth":30,"hourOfDay":15,"minute":46,"second":35}}

So now the question is: how should I write my @TypeConverter so it can save&load my ArrayList?

1
You shouldn't be having an room entity for a list of events but an Entity for a single event.RDO
RDO, Thank you for your reply. Well i have to admit, that I'm confused now... The WeekViewEvent.class belong to the calendar's library. Should I reformulate and annotate the class as: @Entity public class WeekViewEvent {...} along with the fields so it can create a single event and then query the database to get events back as a List (or create ArrayList of Entity events) ? Let me know your thoughts on that, cause i'm looking for simplest solution.Matt
I am not a Room expert but the way I see it is that it is better for you to create a duplicata entity of your weekViewEvent that you can store in room and transform it into the calendar class when you insert/query it. That way you can choose which data you really store and have a simpler room database.RDO

1 Answers

0
votes

Below helps you to find converter for list of your objects.

Just in place of "YourClassName" ,add your Object class.

@TypeConverter
    public String fromValuesToList(ArrayList<**YourClassName**> parentalRating) {
        if (parentalRating == null) {
            return (null);
        }
        Gson gson = new Gson();
        Type type = new TypeToken<ArrayList<**YourClassName**>>() {}.getType();
        return gson.toJson(parentalRating, type);
    }

    @TypeConverter
    public ArrayList<**YourClassName**> toOptionValuesList(String parentalRatingString) {
        if (parentalRatingString == null) {
            return (null);
        }
        Gson gson = new Gson();
        Type type = new TypeToken<List<**YourClassName**>>() {
        }.getType();
        return gson.fromJson(parentalRatingString, type);
    }