【问题标题】:Android Kotlin SharedPreferences saving errorAndroid Kotlin SharedPreferences 保存错误
【发布时间】:2020-11-20 09:54:09
【问题描述】:

我在将数据正确保存到 SharedPreferences 时遇到问题。不知何故最后一个 editor.putString 方法都写进了我所有的偏好里,我也不知道为什么。

这是我的代码:

class MainActivity : AppCompatActivity() {

private var firstnameEditText : EditText ? = null
private var lastnameEditText : EditText ?  = null
private var firstnameTextView : TextView ? = null
private var lastnameTextView : TextView ? = null
private var saveButton : Button? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    firstnameEditText = findViewById(R.id.main_activity_firstname_editText) as EditText
    lastnameEditText = findViewById(R.id.main_activity_lastname_editText) as EditText
    firstnameTextView = findViewById(R.id.main_activity_firstname_textView) as TextView
    lastnameTextView = findViewById(R.id.main_activity_lastname_textView) as TextView
    saveButton = findViewById(R.id.main_activity_save_button) as Button
    saveButton?.setOnClickListener { saveData() }
    loadData()
}

private fun loadData(){
    try {
        val sharedPref = getSharedPreferences( getString(R.string.sharedPrefs) , Context.MODE_PRIVATE) ?: return
        val firstname = sharedPref.getString(getString(R.string.firstname_key), "")
        val lastname = sharedPref.getString(getString(R.string.lastname_key), "")
        firstnameTextView?.text = firstname
        lastnameTextView?.text = lastname
    } catch (ex : Exception){
        Log.d("Main", ex.toString())
    }
}

private fun saveData(){
    try {
        val sharedPref = getSharedPreferences( getString(R.string.sharedPrefs) , Context.MODE_PRIVATE) ?: return
        with(sharedPref.edit()){
            putString(getString(R.string.firstname_key), firstnameEditText?.text.toString())
            putString(getString(R.string.lastname_key), lastnameEditText?.text.toString())
            apply()
        }
        loadData()
    } catch (ex : Exception){
        Log.d("Main", ex.toString())
    }
}

}

点击保存按钮后,结果如下: enter image description here

根据官方文档,我做的一切都是正确的。 enter link description here

任何帮助将不胜感激。 提前致谢。

【问题讨论】:

  • 你能把R.string.firstname_keyR.string.lastname_key的值贴出来吗?

标签: android sharedpreferences


【解决方案1】:

我猜R.string.firstname_keyR.string.lastname_key 的值是一样的。

你能检查一下strings.xml中的那些值

【讨论】:

    【解决方案2】:

    你可以这样试试——

    val sharedPreference =  getSharedPreferences("PREFERENCE_NAME",Context.MODE_PRIVATE)
    var editor = sharedPreference.edit()
    editor.putString("username","Anupam")
    editor.putLong("l",100L)
    editor.commit()
    

    并检索数据-->

    sharedPreference.getString("username","defaultName")
    sharedPreference.getLong("l",1L)
    

    上面你多次初始化getSharedPreferences,用同一个实例获取和保存值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-08
      • 2012-05-30
      • 1970-01-01
      • 1970-01-01
      • 2014-03-05
      • 2020-02-16
      • 2016-06-23
      • 2016-06-15
      相关资源
      最近更新 更多