【问题标题】:How to add new object in Kotlin SharedPreferences如何在 Kotlin SharedPreferences 中添加新对象
【发布时间】:2021-02-18 01:57:23
【问题描述】:

我有这个偏好管理器

class JournalManager {
    lateinit var pref: SharedPreferences
    lateinit var editor: SharedPreferences.Editor
    lateinit var con: Context
    var PRIVATE_MODE: Int = 0

    constructor(con: Context?) {
        if (con != null) {
            this.con = con
        }
        if (con != null) {
            pref = con.getSharedPreferences(PREF_NAME,PRIVATE_MODE)
        }
        editor = pref.edit()
    }

    companion object {
        val PREF_NAME: String = "Journal"
        val KEY_TEXT: String = "text"
    }

    fun createJournalSession(
        text: EditText,
    ) {
        editor.putString(KEY_TEXT, text.toString())
        editor.commit()
    }

    fun getJournalDetails(): Map<String, String>
    {
        var journal: Map<String, String> = HashMap<String, String>()
        pref.getString(KEY_TEXT,null)?.let { (journal as HashMap).put(KEY_TEXT, it) }
        return journal
    }

    fun DeleteJournal() {
        editor.clear()
        editor.commit()

        var i: Intent = Intent(con, JournalActivity::class.java)
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        con.startActivity(i)
    }
}

我正在寻找在其中添加对象的解决方案,但我的应用程序崩溃了,这是我尝试添加对象的示例

lateinit var journalSession: JournalManager

override fun onCreate(savedInstanceState: Bundle?) {...}

fun openDialog() {
        val dialog = MaterialDialog(this)
            .noAutoDismiss()
            .customView(R.layout.layout_new_journal)

        //set initial preferences
        dialog.findViewById<Button>(R.id.save_btn).setOnClickListener{
            val note = dialog.findViewById<EditText>(R.id.new_journal_input)
      

            //add to preference
            journalSession.createJournalSession(
              note
            )
            dialog.dismiss()
        }

        dialog.findViewById<Button>(R.id.cancel_btn).setOnClickListener {
            dialog.dismiss()
        }

        dialog.show()
    }

有什么建议吗?

更新

让我说清楚我在寻找什么:

  1. 一开始我没有任何数据,也没有设备中的共享偏好(共享偏好将在用户保存它的第一个日记时创建)。

2.当用户添加新日志时,它应该像这样存储(作为样本)

journal [{
  note="this was user first note"
}]
  1. 那么当下次用户添加新期刊时,它应该像这样存储(作为样本)
journal [{
  note="this was user first note"
  },
  {note="this was user second note"
}]

等等……

PS 到目前为止,我在网上找到的所有视频、文章都具有相同的逻辑:(他们有一个列表,然后将该列表存储到首选项中!),我的情况不是这样,我没有要存储的列表我的列表将在此期间一个一个地创建,就像任何现实世界的应用程序一样。

现在,知道如何实现,我应该在我的代码中进行哪些更改?

【问题讨论】:

    标签: android kotlin sharedpreferences


    【解决方案1】:

    SharedPreferences 保存原始类型数据和Srting。要保存像 List 这样的对象,您可以使用 GSON 库(由 Google 提供)将对象转换为 JSON String

    1.导入依赖:

    implementation 'com.google.code.gson:gson:2.8.6'
    

    2。基本用法:

    val studentJsonString = Gson().toJson(student)    //object -> String
    
    val student = Gson().fromJson(studentJsonString, Student.class)    //String -> object
    

    3.对于List

    val typeToken = TypeToken<List<Student>>(){}
    
    val students = Gson().fromJson(studentsJsonString, typeToken.getType())
    

    【讨论】:

      猜你喜欢
      • 2014-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-30
      • 2019-08-19
      相关资源
      最近更新 更多