Warm tip: This article is reproduced from stackoverflow.com, please click
android kotlin sharedpreferences

Kotlin SharedPreferences

发布于 2020-03-27 10:22:59

I have a ListView with items created by

 val values = ArrayList<String>().toMutableList()
 val adapter = ArrayAdapter(this, R.layout.listview_text_color, values)

 adapter.add(title_editText.text!!.toString() + " - " + content_editText.text!!.toString())

I need to save those ListView items and reload them again when reopen the app using SharedPreferences (not DB)

This is my actual code about SP:

fun saveData(values: Set<String>) {
    val sharedPref = this.getPreferences(Context.MODE_PRIVATE) ?: return
    with(sharedPref.edit()) {
        putStringSet("test", values)
        putString("title", title_editText.text!!.toString())
        putString("content", content_editText.text!!.toString())
        apply()
    }
}

fun getData() {
    val sharedPref = this.getPreferences(Context.MODE_PRIVATE) ?: return
    val titleData = sharedPref.getString("title", "")
    val contentData = sharedPref.getString("content", "")
    Toast.makeText(this, "$titleData $contentData", Toast.LENGTH_LONG).show()

}

EDIT FOR SOLUTION

After full days of research, I went to solution:

In MainActivity.kt create you ArrayList variable as global:

    class MainActivity : AppCompatActivity() {

    private var values = ArrayList<String>()

    ... //the rest of your code
}

Then add those 2 functions:

 private fun saveData() {
    val sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE)
    val editor = sharedPreferences.edit()
    val gson = Gson()
    val json = gson.toJson(values)
    editor.putString("task list", json)
    editor.apply()
}

private fun loadData() {
    val sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE)
    val gson = Gson()
    val json = sharedPreferences.getString("task list", "")
    val type = object: TypeToken<ArrayList<String>>() {
    }.type

   if(json == null)
       values = ArrayList()
   else
      values = gson.fromJson(json, type)
}

where values is your ArrayList

To save data:

done_fab.setOnClickListener {

        values.add("put your string here")
        adapter.notifyDataSetChanged() //your array adapter
        saveData()
    }

And to load data, simply call the function in your MainActivity:

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

        loadData()

         val adapter = ArrayAdapter(this, R.layout.listview_text_color, values) //your custom arrayAdapter for the listView

        ... //the rest of your code
}
Questioner
Arfmann
Viewed
138
Arfmann 2019-07-08 05:23

SOLUTION

After full days of research, I went to solution:

In MainActivity.kt create you ArrayList variable as global:

    class MainActivity : AppCompatActivity() {

    private var values = ArrayList<String>()

    ... //the rest of your code
}

Then add those 2 functions:

 private fun saveData() {
    val sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE)
    val editor = sharedPreferences.edit()
    val gson = Gson()
    val json = gson.toJson(values)
    editor.putString("task list", json)
    editor.apply()
}

private fun loadData() {
    val sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE)
    val gson = Gson()
    val json = sharedPreferences.getString("task list", "")
    val type = object: TypeToken<ArrayList<String>>() {
    }.type

   if(json == null)
       values = ArrayList()
   else
      values = gson.fromJson(json, type)
}

where values is your ArrayList

To save data:

done_fab.setOnClickListener {

        values.add("put your string here")
        adapter.notifyDataSetChanged() //your array adapter
        saveData()
    }

And to load data, simply call the function in your MainActivity:

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

        loadData()

         val adapter = ArrayAdapter(this, R.layout.listview_text_color, values) //your custom arrayAdapter for the listView

        ... //the rest of your code
}

All this because SharedPreferences doesn't supports ArrayList<String> storing, so passing them with a JSON is the best option