Android Kotlin SharedReferences-保存ListView项并加载它们
我有一个列表视图,其中包含由创建的项目Android Kotlin SharedReferences-保存ListView项并加载它们,android,kotlin,sharedpreferences,Android,Kotlin,Sharedpreferences,我有一个列表视图,其中包含由创建的项目 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()) 要加载数据,只需
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())
要加载数据,只需调用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
}
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
}
将字符串的ArrayList保存到首选项的正确方法是:
.putStringArrayList("test", values)
明白了吗
.getStringArrayList("test")
解决方案
经过整整一天的研究,我找到了解决方案:
在MainActivity.kt中,将ArrayList变量创建为全局变量:
class MainActivity : AppCompatActivity() {
private var values = ArrayList<String>()
... //the rest of your code
}
class MainActivity : AppCompatActivity() {
private var values = ArrayList<String>()
... //the rest of your code
}
要加载数据,只需调用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
}
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
}
所有这些都是因为
SharedReferences
不支持ArrayList
存储,所以用JSON传递它们是最好的选择感谢Arfrmann发布了一个我可以使用的工作解决方案。我必须修改代码以适应片段以及作为可变整数列表的值。所以,我想我应该分享我的想法,这样其他人可以从中受益
class NeedsmetFragment : Fragment() {
private var incorrectList = mutableListOf<Int>()
val PREFS_FILENAME = "com.app.app.prefs"
private fun saveData() {
val sharedPreferences = context!!.getSharedPreferences(PREFS_FILENAME, 0)
val editor = sharedPreferences.edit()
val gson = Gson()
val json = gson.toJson(incorrectList)
editor.putString("incorrectList", json)
editor.apply()
}
private fun loadData() {
val sharedPreferences = context!!.getSharedPreferences(PREFS_FILENAME, 0)
val gson = Gson()
val json = sharedPreferences.getString("incorrectList", "")
val type = object: TypeToken<MutableList<Int>>() {}.type
if(json == null || json == "")
incorrectList = mutableListOf<Int>()
else
incorrectList = gson.fromJson(json, type)
}
//...
x++
incorrestList.add(x)
saveData()
loadData()
thisval = incorrectList[x]
}
类需要片段:片段(){
private var incorrectList=mutableListOf()
val PREFS_FILENAME=“com.app.app.PREFS”
私有数据{
val SharedReferences=context!!.getSharedReferences(首选文件名,0)
val editor=SharedReferences.edit()
val gson=gson()
val json=gson.toJson(不正确的列表)
putString(“incorrectList”,json)
editor.apply()
}
私有数据{
val SharedReferences=context!!.getSharedReferences(首选文件名,0)
val gson=gson()
val json=SharedReferences.getString(“不正确列表”,“不正确列表”)
val type=object:TypeToken(){}.type
如果(json==null | | json==“”)
incorrectList=mutableListOf()
其他的
incorrectList=gson.fromJson(json,类型)
}
//...
x++
不正确列表。添加(x)
saveData()
loadData()
thisval=不正确的列表[x]
}
您能否添加更多的上下文,以便更容易理解如何使用您发布的代码?是的,存储复杂类型的Json字符串(列表、映射..)是一种方法
done_fab.setOnClickListener {
values.add("put your string here")
adapter.notifyDataSetChanged() //your array adapter
saveData()
}
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
}
class NeedsmetFragment : Fragment() {
private var incorrectList = mutableListOf<Int>()
val PREFS_FILENAME = "com.app.app.prefs"
private fun saveData() {
val sharedPreferences = context!!.getSharedPreferences(PREFS_FILENAME, 0)
val editor = sharedPreferences.edit()
val gson = Gson()
val json = gson.toJson(incorrectList)
editor.putString("incorrectList", json)
editor.apply()
}
private fun loadData() {
val sharedPreferences = context!!.getSharedPreferences(PREFS_FILENAME, 0)
val gson = Gson()
val json = sharedPreferences.getString("incorrectList", "")
val type = object: TypeToken<MutableList<Int>>() {}.type
if(json == null || json == "")
incorrectList = mutableListOf<Int>()
else
incorrectList = gson.fromJson(json, type)
}
//...
x++
incorrestList.add(x)
saveData()
loadData()
thisval = incorrectList[x]
}