android - 安卓 - kotlin SharedPreferences保存ListView项,并加载它们

我有一个ListView项目


 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 )重新打开应用程序时重新加载它们,

这是我的代码:


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()



}



在研究之后,我找到了解决方案:

在MainActivity.kt中创建ArrayList变量作为全局变量:


 class MainActivity : AppCompatActivity() {



 private var values = ArrayList<String>()



 ... //the rest of your code


}



然后添加2个函数:


 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)


}



要保存数据:


done_fab.setOnClickListener {



 values.add("put your string here")


 adapter.notifyDataSetChanged() //your array adapter


 saveData()


 }



要加载数据,只需在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


}



时间:

将String的ArrayList保存到首选项的正确方法是:


.putStringArrayList("test", values)



拿着它


.getStringArrayList("test")




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]



}



...