0

我正在尝试使用 Github 中的 TinyDB 来保存列表(mustableListOf),但是当我尝试将字符串保存到列表时,它会在第二个参数上出现错误。

val tinydb = TinyDB(this)
    var todoList = mutableListOf(
        Todo("Write your first TODO statement", false)
    )

    val adapter = TodoAdapter(todoList)
    binding.rvTodos.adapter = adapter
    binding.rvTodos.layoutManager = LinearLayoutManager(this)

    binding.btnAddTodo.setOnClickListener {
        val title = binding.etTodo.text.toString()
        val todo = Todo(title, false)
        todoList.add(todo)
        //puts an error on the "mutableListOf(todoList)"
        tinydb.putListString("TodoList", mutableListOf(todoList))
        adapter.notifyItemInserted(todoList.size - 1)
    }

我该如何解决这个问题?

Github TinyDB 链接:https ://github.com/kcochibili/TinyDB--Android-Shared-Preferences-Turbo

更新的代码:但是,当我的手机上启动该应用程序时,该应用程序一直停止。只有在 TinyDB 中没有存储任何内容时,它才第一次起作用。它不再对第二个参数产生错误,但我觉得它没有按预期的方式存储/工作。谢谢。

val tinydb = TinyDB(this)

    var todoList = mutableListOf(
        Todo("Write your first TODO statement", false)
    )
    // line below is to get the list and set it to recyclerview; however app keeps stopping when invoked
    tinydb.getListObject("TodoList", Todo::class.java)
    val adapter = TodoAdapter(todoList)
    binding.rvTodos.adapter = adapter
    binding.rvTodos.layoutManager = LinearLayoutManager(this)

    binding.btnAddTodo.setOnClickListener {
        val title = binding.etTodo.text.toString()
        val todo = Todo(title, false)
        todoList.add(todo)
        //changed to putListObject
        tinydb.putListObject("TodoList", arrayListOf(todoList))
        adapter.notifyItemInserted(todoList.size - 1)
    }
4

0 回答 0