In my project, I have a list of String. I want to save this List to shared preferences. Any one can help?
data class select(
@SerializedName("items")
var items: MutableList<String>?=null
)
In my project, I have a list of String. I want to save this List to shared preferences. Any one can help?
data class select(
@SerializedName("items")
var items: MutableList<String>?=null
)
You can store list as Json Text in SharedPreference using Gson and then work accordingly
//saving list in Shared Preference
fun setLists(list:ArrayList<String>){
val gson = Gson()
val json = gson.toJson(list)//converting list to Json
editor.putString("LIST",json)
editor.commit()
}
//getting the list from shared preference
fun getList():ArrayList<String>{
val gson = Gson()
val json = preferences.getString("LIST",null)
val type = object :TypeToken<ArrayList<String>>(){}.type//converting the json to list
return gson.fromJson(json,type)//returning the list
}
Don't forget to implement the Gson library in your app level gradle file