我正在构建一个应用程序以将数据存储在共享的首选项文件中问题是我想知道如何在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()
您无法在SharedPreferences中执行此操作,因为它是基于具有键/值的单个映射的文件。
您必须使用数据库,以及SQLite,Room等。>
要保存多个地图,您必须为其值提供唯一的键,每个值也必须是唯一的,例如: