如何保存 ConcatAdapter()?科特林

时间:2021-08-01 23:51:29

标签: kotlin android-recyclerview android-concatadapter

如何保存这种数据类型“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()
}}

0 个答案:

没有答案