如何保存这种数据类型“concat Adapter()”?我用 json 尝试过 sharedPreferences 但它没有用。
我想保存一个带有不同 ViewHolders 的 RecyclerView 以在重新打开应用程序后显示,但我找不到方法。
class Mainactivity : AppCompatactivity(){
var userList: MutableList<ExampleItem> = mutableListOf()
var userList2: MutableList<ExampleItem> = mutableListOf()
var adapterLst : concatAdapter = concatAdapter()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recycler_view.layoutManager=GridLayoutManager(this,2)
recycler_view.setHasFixedSize(true)
userList2.add(ExampleItem("cuatro","cinco"))
userList.add(ExampleItem("cuatro","cuatro"))
adapterLst = concatAdapter(ExampleAdapter(this,userList),typ2Adapter(this,userList2))
recycler_view.adapter = adapterLst
save.setOnClicklistener {
saveData()
}
}
private fun saveData() {
val sharedPreferences = getSharedPreferences("shared preferences",Context.MODE_PRIVATE)
val editor = sharedPreferences.edit()
val gson = Gson()
val json = gson.toJson(adapterLst)
editor.putString("recycler list",json)
editor.apply()
}}