[英]Saving single object properties in sharedPreferences
有没有办法使用 SharedPreferences 来保存每个对象,然后再加载该特定对象? 我有一个我设法用 SharedPreferences 保存的对象列表(使用 RecyclerView 显示它们),当我从该列表中选择这些对象中的任何一个时,将打开一个新活动,该对象将作为参数传递。 我想要做的是添加到该特定对象的列表中(每个对象都有一个 Mutable List 属性),然后将该列表存储在 SharedPreferences 中。 我已经设法做到了,但问题是我的列表将被保存到一个“全局”列表中,并且每个项目都将加载完全相同的列表。 如何将 SavedPreference 与该特定对象关联,然后也加载该特定对象的列表? 我附上了应用程序的图片,以便您可以更好地理解我的意思。
SingleListActivity.kt
package com.example.simpleshoppinglist
import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import com.example.simpleshoppinglist.adapter.ShoppingItemAdapter
import com.example.simpleshoppinglist.data.Datasource
import com.example.simpleshoppinglist.model.ShoppingItem
import com.example.simpleshoppinglist.model.SingleLista
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
val activityIntentId: Int = 1
class SingleListActivity : AppCompatActivity() {
//Empty init
var singleList = SingleLista ("Placeholder")
var listSource = singleList.listOfShoppingItems
var shoppingItemAdapter = ShoppingItemAdapter(this, singleList.listOfShoppingItems)
lateinit var lblTest: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_single_list)
// LABEL ZA TESTIRANJE - 2 LINIJE
val lblTestni: TextView = findViewById(R.id.TESTNILABEL)
lblTest = lblTestni
val getObjectFromRecyclerOnClick = intent.getSerializableExtra("Extra_object") as SingleLista
singleList = getObjectFromRecyclerOnClick
loadData()
listSource = singleList.listOfShoppingItems
shoppingItemAdapter = ShoppingItemAdapter(this, singleList.listOfShoppingItems)
var recyclerViewSingle = findViewById<RecyclerView>(R.id.recycler_view_single)
val btnAddShoppingItem: Button = findViewById(R.id.btnDodajNamirnicu)
recyclerViewSingle.adapter = shoppingItemAdapter
btnAddShoppingItem.setOnClickListener {
val intent = Intent(this, AddShoppingItemActivity::class.java)
intent.putExtra("SinglLista", singleList)
this.startActivityForResult(intent, activityIntentId)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == activityIntentId) {
if (resultCode == RESULT_OK) {
//var testniLabel: TextView = findViewById(R.id.TESTNILABEL)
//testniLabel.text = "TEST LABEL"
var shoppingItemName = data?.getSerializableExtra("NazivNamirnice") as String
var shoppingItemQuantity = data?.getSerializableExtra("KolicinaNamirnice") as String
addShoppingItem(
shoppingItemName,
shoppingItemQuantity,
shoppingItemAdapter,
)
saveData()
}
}
}
fun saveData (){
var sharedPreferences: SharedPreferences = getSharedPreferences("Liste_u_objektu", MODE_PRIVATE)
var editor: SharedPreferences.Editor = sharedPreferences.edit()
var gson = Gson()
var json: String = gson.toJson(singleList.listOfShoppingItems)
editor.putString("Spremi_Objekt", json)
editor.apply()
//lblTest.text = singleList.naziv TEST LABEL
}
fun loadData(){
var sharedPreferences: SharedPreferences = getSharedPreferences("Liste_u_objektu", MODE_PRIVATE)
var gson = Gson()
if (sharedPreferences.getString("Spremi_Objekt", null) != null){
var json: String = sharedPreferences.getString("Spremi_Objekt", null)!!
val turnsType = object : TypeToken<MutableList<ShoppingItem>>() {}.type
singleList.listOfShoppingItems = gson.fromJson(json,turnsType)
shoppingItemAdapter.notifyDataSetChanged()
}
else{
listSource.clear()
}
}
fun addShoppingItem(
shoppingItemName: String,
shoppingItemQuantity: String,
adapter: ShoppingItemAdapter,
) {
singleList.listOfShoppingItems.add(ShoppingItem(
shoppingItemName,
shoppingItemQuantity.toInt()
))
adapter.notifyDataSetChanged()
}
}
编辑:也许每个对象都应该有自己的 SharedPreference ? 如果我为每个对象创建一个唯一标识符会怎样?
编辑 2:我设法通过为 SharedPreference 键创建唯一标识符来解决该问题。 在我的情况下,它是购物清单名称,然后我会阻止用户添加 2 个具有相同名称的清单。
我已经通过为 SharedPreference 键创建唯一标识符来解决这个问题。 在我的情况下,它是购物清单名称,然后我会阻止用户添加 2 个具有相同名称的清单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.