Kotlin SharedPreferences-保存ListView项并加载它们 [英] Kotlin SharedPreferences - Save ListView items and load them
问题描述
我有一个ListView,其中包含
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())
我需要保存这些ListView项,并在使用SharedPreferences(不是DB)重新打开应用程序时再次重新加载它们
I need to save those ListView items and reload them again when reopen the app using SharedPreferences (not DB)
这是我关于SP的实际代码:
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:
在MainActivity.kt中,将ArrayList变量创建为全局变量:
In MainActivity.kt create you ArrayList variable as global:
class MainActivity : AppCompatActivity() {
private var values = ArrayList<String>()
... //the rest of your code
}
然后添加这两个功能:
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)
}
其中values
是您的ArrayList
where values
is your ArrayList
要保存数据:
done_fab.setOnClickListener {
values.add("put your string here")
adapter.notifyDataSetChanged() //your array adapter
saveData()
}
要加载数据,只需在MainActivity中调用该函数即可:
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
}
推荐答案
解决方案
经过一整天的研究,我找到了解决方案:
SOLUTION
After full days of research, I went to solution:
在MainActivity.kt中,将ArrayList变量创建为全局变量:
In MainActivity.kt create you ArrayList variable as global:
class MainActivity : AppCompatActivity() {
private var values = ArrayList<String>()
... //the rest of your code
}
然后添加这两个功能:
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)
}
其中values
是您的ArrayList
where values
is your ArrayList
要保存数据:
done_fab.setOnClickListener {
values.add("put your string here")
adapter.notifyDataSetChanged() //your array adapter
saveData()
}
要加载数据,只需在MainActivity中调用该函数即可:
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
}
所有这些都是因为SharedPreferences
不支持ArrayList<String>
存储,因此最好使用JSON传递它们
All this because SharedPreferences
doesn't supports ArrayList<String>
storing, so passing them with a JSON is the best option
这篇关于Kotlin SharedPreferences-保存ListView项并加载它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!