skip to Main Content

I have a global object that store 2 custom class objects, something like this:

 /////// Separated object file ////////

    object ApplicationData {
    var profiledata:ProfileData = ProfileData(null)
    var OVOlists = ArrayList<OVOList>()
}
/////// Separated  class file ////////

 class OVOList (
    private var OVOIcon: Int,
    private var OVOName: String?,
    private var OVOPlace: String?,
    private var OVOstatus: Int
) :Parcelable {
    private var Humidity:String? =""
    private var Temperature:String? = ""
    private var PH:String? =""
    private var WaterLvl :String? = ""

    constructor(parcel: Parcel) : this(
        parcel.readValue(Int::class.java.classLoader) as Int,
        parcel.readString(),
        parcel.readString(),
        parcel.readValue(Int::class.java.classLoader) as Int
    ) {
        Humidity = parcel.readString()
        Temperature = parcel.readString()
        PH = parcel.readString()
        WaterLvl = parcel.readString()
    } ... + setters , getters and parcel lines

/////// separated class file ////////
class ProfileData(private var email:String?):Parcelable {
    private var token:String? =""
    private var profile_image: String? =""
    private var profile_image_path:String? = ""
    private var nombre:String? = ""
    private var uid:String? = ""

    constructor(parcel: Parcel) : this(
        parcel.readString(),
    ) {
        token=parcel.readString()
        profile_image = parcel.readString()
        profile_image_path = parcel.readString()
        nombre = parcel.readString()
        uid=parcel.readString()
    } ... + setters,getters and parcel lines

The classes are parcelable, because i was moving some information via bundles, but now im using this global object so there is no need to keep them like that.
But the question is how to store the whole object into memory, i have try the Gson/preferences aproach but i cannot make it work :(, it doesnt store the object, maybe its because it has 2 custom class objects inside, i dont think the parcelable attribute should affect. I made something like this:

//write//
mprefs.edit().putString("MyappData",gson.toJson(ApplicationData)).apply()
//read//
String json = mprefs.getString("MyappData", "")
val obj = gson.fromJson(json, ApplicationData::java.class)
ApplicationData.profiledata = obj.profiledata
ApplicationData.OVOlists = obj.OVOlists

It seems that its failing in the writing part , any ideas what to do?

2

Answers


  1. Chosen as BEST ANSWER

    Thanks to all your help, i made this work with Gson, i created inside the object a function that stores and retrieves the data from/to pref. i just pass the context to it, i think was not working properly because of the parcelable part inside the classes. The final object is this:

    object ApplicationData {
    var profiledata:ProfileData = ProfileData(null)
    var OVOlists = ArrayList<OVOList>()
    
    fun save(context: Context){
        val sharedPreference = context.getSharedPreferences("OVO_PREF", Context.MODE_PRIVATE)
        val prefsEditor = sharedPreference.edit()
        val gson = Gson()
        val json = gson.toJson(ApplicationData)
        prefsEditor.putString("AppData", json)
        Log.e("saving in preferences",json)
        prefsEditor.apply()
    }
    fun retrieveData(context: Context):Boolean{
        val sharedPreference = context.getSharedPreferences("OVO_PREF", Context.MODE_PRIVATE)
        val gson = Gson()
        val json = sharedPreference.getString("AppData", "")
        val obj = gson.fromJson(json, ApplicationData::class.java)
        if(obj!=null){
            profiledata = obj.profiledata
            OVOlists = obj.OVOlists
            //Log.e("retrieve info",obj.OVOlists[0].getOVOName()!!)
            return true
        }else{
            return false }
    }
    

    The two custom classes now are regular and normal classes, with setters and getters. So the main object can save its own data calling ApplicationData.save(context)


  2. You can save them as Strings (json format ) and the covert to Object ,as you are doing , I think you should use data classes and use @Parcelize and avoid using too much code

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search