SharedPreferences将数据保存到对象列表时出现问题



我很难弄清楚我的代码出了什么问题。我使用RecyclerView和SharedPreferences的组合来创建一个简单的购物列表。这个想法是首先输入购物列表的名称,当用户点击添加的列表的名称(选择它(时,一个新的活动将打开,并传递该对象(通过serializableExtra检索(,您将能够将购物项目添加到该特定对象的列表中。

在我保存购物列表对象列表的主要活动中,SharedPreferences(与gson一起(工作得很好,然而,当我试图在第二个活动中为购物项目列表复制该内容时,我的应用程序崩溃了。当我的构建日志内存不足时,它似乎进入了一个循环。

我已经调试了代码,问题发生在函数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(listSource)
editor.putString("Spremi_Objekt", json)
editor.apply()
}

SingleListActivity.kt

package com.example.simpleshoppinglist
import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import com.example.simpleshoppinglist.adapter.ShoppingItemAdapter
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, listSource)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_single_list)
val getObjectFromRecyclerOnClick = intent.getSerializableExtra("Extra_object") as SingleLista
singleList = getObjectFromRecyclerOnClick
listSource = singleList.listOfShoppingItems
loadData()
shoppingItemAdapter = ShoppingItemAdapter(this, listSource)
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(listSource)
editor.putString("Spremi_Objekt", json)
editor.apply()
}
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
listSource = gson.fromJson(json,turnsType)
}
else{
listSource.clear()
}
}
fun addShoppingItem(
shoppingItemName: String,
shoppingItemQuantity: String,
adapter: ShoppingItemAdapter,
) {
listSource.add(ShoppingItem(
shoppingItemName,
shoppingItemQuantity.toInt(),
singleList
))
adapter.notifyDataSetChanged()
}
}

AddShoppingItemActivity.kt

package com.example.simpleshoppinglist
import android.app.Activity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
class AddShoppingItemActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_dodaj_namirnicu)
val shoppingItemName: EditText = findViewById(R.id.nazivNamirniceEditText)
val shoppingItemQuantity: EditText = findViewById(R.id.kolicinaNamirniceEditText)
val btnAddShoppingItem: Button = findViewById(R.id.btnSpremiNamirnicu)

btnAddShoppingItem.setOnClickListener {
val intent = Intent(this, SingleListActivity::class.java)
intent.putExtra("NazivNamirnice",shoppingItemName.text.toString())
intent.putExtra("KolicinaNamirnice",shoppingItemQuantity.text.toString())
setResult(Activity.RESULT_OK,intent)
finish()
}
}
}

在我看来,您的字符串可能太大了。虽然共享偏好对xml文件的实际大小没有限制,但有些人遇到了问题:共享偏好-单个值的最大长度

您真的应该使用sqlite或Room来存储您的列表。

我最终解决了这个问题。如果有人遇到这个问题,请确保仔细检查每个类,以确定是否正确设置了属性。答案与此类似:Servlet Gson((.toJson无限循环

相关内容

  • 没有找到相关文章

最新更新