我的 RecyclerView 一次又一次地从房间数据库中获取数据

My RecyclerView fetching data from Room Database Again and again

我的 recyclerView 重复显示数据,但我只想要一次数据。我正在从房间数据库中获取数据。当我在数据库检查中看到它时,我的数据库完美地存储了数据。当我滚动 recyclerview 获取重复数据时

class MainActivity2 : AppCompatActivity() {

    lateinit var repository: Repository
    lateinit var daoClass: DaoClass
    lateinit var adapter: adapterClass
    lateinit var databaseClass: DatabaseClass
    lateinit var factory: ViewModelFactory
    lateinit var viewmodelClass: ViewmodelClass
    private var newList = mutableListOf<EntityClass>()
    private var list = mutableListOf<EntityClass>()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)

        databaseClass = DatabaseClass.getDatabase(this)
        repository = Repository(databaseClass)
        daoClass = databaseClass.getDao()
        factory = ViewModelFactory(repository)
        viewmodelClass = ViewModelProvider(this, factory)[ViewmodelClass::class.java]

                insertData(list)

        if (newList.isEmpty()) {
            getAllData()
        }
    }

    private fun getAllData() {


        newList.clear()
        viewmodelClass.getData().observe(this, Observer {
            newList.addAll(it)
            setRecyclerView()
        })
    }

    private fun setRecyclerView() {

        adapter = adapterClass(newList)
        recyclerview.adapter = adapter
        recyclerview.layoutManager = LinearLayoutManager(this)

    }

    private fun insertData(list: MutableList<EntityClass>) {

        list.clear()
            val item1 = EntityClass("01 March 2022", 5)
            list.add(item1)
            val item2 = EntityClass("02 March 2022", 7)
            list.add(item2)
            val item3 = EntityClass("03 March 2022", 4)
            list.add(item3)
            val item4 = EntityClass("04 March 2022", 0)
            list.add(item4)
            val item5 = EntityClass("05 March 2022", 6)
            list.add(item5)
            val item6 = EntityClass("06 March 2022", 5)
            list.add(item6)
            val item7 = EntityClass("07 March 2022", 6)
            list.add(item7)
            val item8 = EntityClass("08 March 2022", 4)
            list.add(item8)
            val item9 = EntityClass("09 March 2022", 3)
            list.add(item9)
            val item10 = EntityClass("10 March 2022", 9)
            list.add(item10)
            val item11 = EntityClass("11 March 2022", 3)
            list.add(item11)
            val item12 = EntityClass("12 March 2022", 5)
            list.add(item12)
            val item13 = EntityClass("13 March 2022", 0)
            list.add(item13)
            val item14 = EntityClass("14 March 2022", 10)
            list.add(item14)

        CoroutineScope(Main).launch {
            viewmodelClass.deleteData()
            list.forEach {
                val datadb= EntityClass(it.Date,it.Sessions)
                viewmodelClass.insertData(datadb)
            }

        }

        }
    }

我原以为只保存显示我插入的那 14 个列表项,但它重复显示这 14 个项目。

您需要先清除列表,然后再将数据添加到列表中。

private fun getAllData() {
    viewmodelClass.getData().observe(this, Observer {
        newList.clear()
        newList.addAll(it)
        setRecyclerView()
    })
}