last day (15 days later) » 

7:44 PM
1
A: Storing an ArrayList of Custom Objects when App closes/Retrieving when opens

mmloolootry this: Change: globaldata.setMachineProfileList(gson.fromJson(machineProfileListJSON, ArrayList.class)); globaldata.setContactInfoList(gson.fromJson(contactInfoListJSON, ArrayList.class)); globaldata.setEventInfoList(gson.fromJson(eventInfoListJSON, ArrayList.class))...

 
Tried that out; does not work for any of the Type types; there are four possible imports...
 
look my updated answer!
 
Actually @mmlooloo I think my solution might work but let me describe my problem. Whenever I open the app I can create the data fine - but I think if I switch to a second activity - like creating a contact object and adding to the ArrayList, when I go back into the MainActivity I think the retrieveGlobalDataFromStorage(); overwrites the data I just made!
Do you know if there is a way I can call the store and retrieve methods when the app closes or opens; that way it is not dependent on the Activity's lifecycle?
 
call store at the onPause and call retrieve at onCreate of each activity.
 
I got it working for that but now I have a new issue; when it retrieves the data - instead of putting it back into proper POJO's; it fills my ArrayList with JSON formatted data (not reconstructing the objects)
 
7:44 PM
have you tried my first answer ! (TypeToken)
 
Which Type should I import because each one causes several errors?
 
import java.lang.reflect.Type; + import com.google.gson.Gson; import com.google.gson.reflect.TypeToken;
 
Did that: - I get The method setMachineProfileList(ArrayList<MachineProfile>) in the type GlobalData is not applicable for the arguments (Object)
 
hmm no idea right now
i think you do not get array
 
Yea
Do you think it would be better to do an AsyncTask to iterate through the ArrayList and store each object?
and then another to read?
or is that obnoxious
 
7:47 PM
it is to early to decide to use async
your problem is
using Gson
you can not read it
correctly
take a look at two samples
 
roger reading them now
Do you want to see one of my POJO class files?
package com.nanospark.cnc;

import java.util.ArrayList;

public class MachineProfile {
String profileName;
RGBClass RGBVals;
ArrayList<Event> attachedEvents = new ArrayList<Event>();

public MachineProfile(String profileName, RGBClass RGBVals, ArrayList<Event> attachedEvents) {
this.profileName = profileName;
this.RGBVals = RGBVals;
this.attachedEvents = attachedEvents;
}

public String getProfileName() {
return profileName;
}

public void setProfileName(String profileName) {
this.profileName = profileName;
 
i do not think it has any problem
but do not worry
the one question that if
you ask you will
certainly get
 
I think I fixed it one second let me check
 
answer on the So
is PARSING JSON PROBLEM
 
roger
 
7:54 PM
just ask it and you will get a lot of answer. title it
 
fixed it
 
like How to deserialize this json with GSON
wow googd luck whats the problem?
 
if I changed setMachineProfileList((gson.fromJson(machineProfileListJSON,machineProfileListTy‌​pe));
to this:
machineProfileList = (gson.fromJson(machineProfileListJSON, machineProfileListType));
it works fine
 
Happy coding :-)
Bye
 
thanks for all your help
have a good day
:)
 

last day (15 days later) »