【发布时间】:2020-03-11 09:23:41
【问题描述】:
我正在构建一个应用程序来将数据存储在共享首选项文件中 问题是我想知道如何在 sharedpref 文件中添加新行(映射),但不能编辑相同的映射或更新相同的映射键、值对
当我在添加新数据后检查它所在位置的 sharedpref 文件时,我发现相同的地图正在更新并且没有添加新地图(或行)
MainActivity.kt
//start sharedpreference to save data
val sharedPrefFile = "goldinvesto" //filename to save
val collectdata:SharedPreferences = this.getSharedPreferences(sharedPrefFile, Context.MODE_PRIVATE)
val editor:SharedPreferences.Editor=collectdata.edit()
editor.putString("start_date",save_date)
editor.putString("amount",save_amount_txt)
editor.putString("currency",spin.selectedItem.toString())
editor.putString("karat",spin2.selectedItem.toString())
editor.putString("enter_price",save_enter_price)
editor.apply()
editor.commit()
【问题讨论】:
-
行/列保存数据,建议使用sqlite
-
如果你想坚持 SharedPreference,每个地图都是一个单独的文件。
标签: android kotlin sharedpreferences