Home > Software design >  Android Kotlin - save and retrieve ArrayList of custom model with SharedPreferences
Android Kotlin - save and retrieve ArrayList of custom model with SharedPreferences

Time:11-11

Before you mark it as duplicate, I already seen similar questions and none of the solutions work when I try them!

This is how I tried it based on the answers in the "solutions":

Saving:

val gson = Gson()
val hiddenUserList: ArrayList<HiddenUserModel> = ArrayList()
hiddenUserList.add(HiddenUserModel(777L, "steveee"))
hiddenUserList.add(HiddenUserModel(888L, "donbimbo"))
val json = gson.toJson(hiddenUserList)
prefs.edit().putString("hiddenUser", json).apply()

So far it's correct I guess?

Now when I try to retrieve and read it:

if(prefs.getString("hiddenUser", null) != null){
    val hiddenUserListBack: ArrayList<HiddenUserModel> = ArrayList()
    val gsonBack = Gson()
    val jsonBack: String = prefs.getString("hiddenUser", null)!!
    val obj: ArrayList<HiddenUserModel> = gsonBack.fromJson(jsonBack, hiddenUserListBack::class.java)

    Log.d(tagg, obj[0].nickname   " "   obj[1].nickname) 
}

I get:

java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.website.blaaa.models.HiddenUserModel

What is wrong here?

I'd also accept solutions without gson as long as it works and doesn't contain tons of workaround

CodePudding user response:

Try letting Gson to decide on the actual type of collection to deserialize the list to. For example, create a class with a List field:

data class UserBag(val users: List<HiddenUserModel>)

Then put your users there, and serialize and save:

...
val json = gson.toJson(UserBag(users = hiddenUserListBack))
prefs.edit().putString("hiddenUser", json).apply()

The just serialize/deserialize an instance of that class, so you don't care of the actual runtime of List used:

val userBag = gsonBack.fromJson(jsonBack, UserBag::class.java)
userBag.users.forEach {
    Log.d(tagg, "User nickname: ${it.nickname}") 
}

  • Related