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 使用Kotlin从另一个列表中提取的字符串创建列表_Android_Kotlin_Collections_Sharedpreferences - Fatal编程技术网

Android 使用Kotlin从另一个列表中提取的字符串创建列表

Android 使用Kotlin从另一个列表中提取的字符串创建列表,android,kotlin,collections,sharedpreferences,Android,Kotlin,Collections,Sharedpreferences,在我的应用程序中,我将此类用作模型: class ExpenseItem (val concept: String, val amount: String, val months: List<String>, val type: Type, val cards_image: Int, val payDay: Int, val notes: String) { enum class Type {RECURRENT, VARIABLE} } 如您所见,其中一个模型字段也是字符

在我的应用程序中,我将此类用作模型:

class ExpenseItem (val concept: String, val amount: String, val months: List<String>, val type: Type, val cards_image: Int, val payDay: Int, val notes: String) {

    enum class Type {RECURRENT, VARIABLE}
}
如您所见,其中一个模型字段也是字符串类型列表,以防它很重要

我的目的是将这个列表转换为字符串,将其另存为SharedReference,然后使用从SharedReference检索到的字符串创建一个新列表。 要将列表转换为字符串,我可以使用toString或joinToString,这两种方法都可以获得最佳结果。 当我想从字符串创建一个新列表时,我遇到了问题。 我可以使用
List
类型的列表,但不能使用
List
类型的列表


有人能帮我吗?

简单的方法,你可以使用
Gson
库,将它添加到build.gradle,它会将你的列表序列化到
JSON
并保存到
SharePreference

  implementation 'com.google.code.gson:gson:2.8.6'
public void保存项(列表项){
if(items!=null&&!items.isEmpty()){
字符串json=new Gson().toJson(items);
mSharedPreferences.edit().putString(“items”,json).apply();
}
}
公共列表getItems(){
String json=mSharedPreferences.getString(“items”,“items”);
if(TextUtils.isEmpty(json))返回Collections.emptyList();
Type Type=new-TypeToken(){
}.getType();
List result=new Gson().fromJson(json,类型);
返回结果;
}

简单的方法,您可以使用
Gson
库,将其添加到build.gradle,它会将您的列表序列化为
JSON
,并将其保存到
SharePreference

  implementation 'com.google.code.gson:gson:2.8.6'
public void保存项(列表项){
if(items!=null&&!items.isEmpty()){
字符串json=new Gson().toJson(items);
mSharedPreferences.edit().putString(“items”,json).apply();
}
}
公共列表getItems(){
String json=mSharedPreferences.getString(“items”,“items”);
if(TextUtils.isEmpty(json))返回Collections.emptyList();
Type Type=new-TypeToken(){
}.getType();
List result=new Gson().fromJson(json,类型);
返回结果;
}

您需要使用Gson()类。 首先,像下面这样上课

class WhateverName(var generalExpensesList: MutableList<ExpenseItem> = mutableListOf()) 
Gson().toJson(WhateverName(arrayListOf()))
它将为您提供字符串并将其作为字符串保存到首选项。 从首选项中检索字符串后,需要再次将其转换为该对象,以便使用下面的代码

Gson().fromJson("string", WhateverName::class.java)

它将为您提供WhateverName类,您可以从中访问列表。

您需要使用Gson()类。 首先,像下面这样上课

class WhateverName(var generalExpensesList: MutableList<ExpenseItem> = mutableListOf()) 
Gson().toJson(WhateverName(arrayListOf()))
它将为您提供字符串并将其作为字符串保存到首选项。 从首选项中检索字符串后,需要再次将其转换为该对象,以便使用下面的代码

Gson().fromJson("string", WhateverName::class.java)

它将为您提供WhateverName类,您可以从中访问您的列表。

使用Gson将其映射到Json并保存到SharePreference,您可以轻松检索列表您是指Gson库,对吗?我从来没用过。我将不得不进行调查,看看它是如何工作的。您知道应用于此的任何实现示例吗?。感谢您在sharedPrefs中的实例
[1,2]
。是否要将此内容表单字符串转换为列表?否,sharedprefs是一个包含模型类“ExpenseItem”数据的字符串,该字符串是我要转换为列表类型的字符串使用Gson将其映射为Json并保存到SharePreference,您可以轻松检索列表您是指Gson库,对吗?我从来没用过。我将不得不进行调查,看看它是如何工作的。您知道应用于此的任何实现示例吗?。感谢您在sharedPrefs中的实例
[1,2]
。是否要将此内容表单字符串转换为列表?否,sharedprefs是一个包含模型类“ExpenseItem”数据的字符串,该字符串是我要转换为列表类型列表的字符串