androidkotlinarraylistandroid-recyclerviewandroid-listadapter

how to add data in ListAdapter?


In ListAdapter, how can I add data to existing list?

submitList() will only replace existing list with new one, maybe a method to updating data something like this

adapter.addNewItem(list)

Solution

  • Instead of modifying data in ListAdapter, always update the datasource i.e list in your case and submit the updated list to ListAdapter

    //assume you have this either in your view model or where ever it is
    val dataList = ArrayList<String>()
    
    fun updateData(newData: List<Data>){
        dataList.addAll(newData) // add new data to existing data
        adapter.submitList(dataList) //submit the data again
    }