Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android studio 如何保存列表<;字符串>;在kotlin中共享数据引用?_Android Studio_Kotlin_Sharedpreferences - Fatal编程技术网

Android studio 如何保存列表<;字符串>;在kotlin中共享数据引用?

Android studio 如何保存列表<;字符串>;在kotlin中共享数据引用?,android-studio,kotlin,sharedpreferences,Android Studio,Kotlin,Sharedpreferences,在我的项目中,我有一个字符串列表。 我想将此列表保存到共享首选项。 有人能帮忙吗 data class select( @SerializedName("items") var items: MutableList<String>?=null ) 数据类选择( @序列化名称(“项目”) 变量项:可变列表?=null ) 您可以使用Gson将列表存储为SharedReference中的Json文本,然后进行相应的操作 //saving list in Shared P

在我的项目中,我有一个字符串列表。 我想将此列表保存到共享首选项。 有人能帮忙吗

data class select(
    @SerializedName("items")
    var items: MutableList<String>?=null
)
数据类选择(
@序列化名称(“项目”)
变量项:可变列表?=null
)

您可以使用Gson将列表存储为SharedReference中的Json文本,然后进行相应的操作

//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
    }
//在共享首选项中保存列表
趣味集合列表(列表:ArrayList){
val gson=gson()
val json=gson.toJson(list)//将list转换为json
putString(“LIST”,json)
editor.commit()
}
//从共享首选项获取列表
趣味getList():ArrayList{
val gson=gson()
val json=preferences.getString(“LIST”,null)
val type=object:TypeToken(){}.type//将json转换为列表
return gson.fromJson(json,type)//返回列表
}

别忘了在应用程序级gradle文件中实现Gson库

这是否回答了您的问题?我需要kotlin的描述。。