使用 Kotlin 从另一个列表中提取的字符串创建一个列表
Create a list from a string extracted from another list with Kotlin
在我的应用程序中,我使用这个 class 作为模型:
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}
}
并且使用这个模型,我创建了一个可变列表
var generalExpensesList: MutableList<ExpenseItem> = mutableListOf()
然后我添加项目
val currentExpense = ExpenseItem(
concept,
amount,
listOfMonths,
enumtype,
card_image_number,
payday.toInt(),
notes
)
generalExpensesList.add(currentExpense)
可以看到,其中一个模型字段也是String类型的列表,万一重要呢
嗯,我的目的是将此列表转换为字符串,将其保存为共享首选项,然后使用从共享首选项中检索到的字符串创建一个新列表。
要将列表转换为字符串,我可以使用 toString 或 joinToString,它们都给我一个最佳结果。
当我想从 String 创建一个新列表时遇到问题。
我可以用 List<String>
类型的列表来做,但不能用 List<ExpenseItem>
类型的列表
有人可以帮我解决这个问题吗?
简单的方法,你可以使用Gson
库,将它添加到build.gradle,它会将你的列表序列化为JSON
并保存到SharePreference
implementation 'com.google.code.gson:gson:2.8.6'
public void saveItems(List<ExpenseItem> items) {
if (items != null && !items.isEmpty()) {
String json = new Gson().toJson(items);
mSharedPreferences.edit().putString("items", json).apply();
}
}
public List<ExpenseItem> getItems() {
String json = mSharedPreferences.getString("items", "");
if (TextUtils.isEmpty(json)) return Collections.emptyList();
Type type = new TypeToken<List<ExpenseItem>>() {
}.getType();
List<ExpenseItem> result = new Gson().fromJson(json, type);
return result;
}
您需要使用Gson()class。
首先使 class 像下面这样
class WhateverName(var generalExpensesList: MutableList<ExpenseItem> = mutableListOf())
并将您的列表传递给此 class 并制作此 class 的对象,然后需要像下面那样制作它的字符串
Gson().toJson(WhateverName(arrayListOf()))
它将为您提供字符串并将其作为字符串保存到首选项。
从首选项中检索字符串后,您需要再次将其转换为该对象,以便在下面的代码中使用。
Gson().fromJson("string", WhateverName::class.java)
它将为您提供 WhateverName 的 class,您可以从中访问您的列表。
在我的应用程序中,我使用这个 class 作为模型:
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}
}
并且使用这个模型,我创建了一个可变列表
var generalExpensesList: MutableList<ExpenseItem> = mutableListOf()
然后我添加项目
val currentExpense = ExpenseItem(
concept,
amount,
listOfMonths,
enumtype,
card_image_number,
payday.toInt(),
notes
)
generalExpensesList.add(currentExpense)
可以看到,其中一个模型字段也是String类型的列表,万一重要呢
嗯,我的目的是将此列表转换为字符串,将其保存为共享首选项,然后使用从共享首选项中检索到的字符串创建一个新列表。
要将列表转换为字符串,我可以使用 toString 或 joinToString,它们都给我一个最佳结果。
当我想从 String 创建一个新列表时遇到问题。
我可以用 List<String>
类型的列表来做,但不能用 List<ExpenseItem>
有人可以帮我解决这个问题吗?
简单的方法,你可以使用Gson
库,将它添加到build.gradle,它会将你的列表序列化为JSON
并保存到SharePreference
implementation 'com.google.code.gson:gson:2.8.6'
public void saveItems(List<ExpenseItem> items) {
if (items != null && !items.isEmpty()) {
String json = new Gson().toJson(items);
mSharedPreferences.edit().putString("items", json).apply();
}
}
public List<ExpenseItem> getItems() {
String json = mSharedPreferences.getString("items", "");
if (TextUtils.isEmpty(json)) return Collections.emptyList();
Type type = new TypeToken<List<ExpenseItem>>() {
}.getType();
List<ExpenseItem> result = new Gson().fromJson(json, type);
return result;
}
您需要使用Gson()class。 首先使 class 像下面这样
class WhateverName(var generalExpensesList: MutableList<ExpenseItem> = mutableListOf())
并将您的列表传递给此 class 并制作此 class 的对象,然后需要像下面那样制作它的字符串
Gson().toJson(WhateverName(arrayListOf()))
它将为您提供字符串并将其作为字符串保存到首选项。 从首选项中检索字符串后,您需要再次将其转换为该对象,以便在下面的代码中使用。
Gson().fromJson("string", WhateverName::class.java)
它将为您提供 WhateverName 的 class,您可以从中访问您的列表。