How to rebind item in RecyclerView when the data changed in Kotlin?

The secret maybe hide here:

class CustomAdapter (val backupItemList: List)

When you init an instance of CustomAdapter, the value is copied to property backupItemList, rather than assigned the reference. Therefore, when you change property allList of UIMain, the backupItemList won't change as you expected.

The solution is simple, as Ganesh Tikone wrote: add an method to update backupItemList.

fun updateData(data: List<MovieModel>) {
    backupItemList.clear()
    backupItemList.addAll(data)
    notifyDataSetChanged()
}

and change Code D to:

//Code D     
   if (resultCode == RESULT_OK) {
      allList=SettingHandler().getListAllSetting()  
      mCustomAdapter.updateData(allList)    
      mCustomAdapter.setSelectedItem(selectedBackupItem)                
   }  

Have a try.


In your Activity onCreate method

    override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.layout_main)                
    
            allList = ArrayList< MSetting>()
            mCustomAdapter = CustomAdapter(mChildList)
            mRecyclerView.layoutManager = LinearLayoutManager(context)
            mRecyclerView.adapter = mCustomAdapter
    }

Now in onActivityResult

     public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {

           //Code C          
           if (resultCode == RESULT_OK) {
              if(data != null){
                 allList.clear()
                 allList.addAll(SettingHandler().getListAllSetting())
                 mCustomAdapter.notifyDataSetChanged()
                 mCustomAdapter.setSelectedItem(selectedBackupItem) 
              } 
           }
        
      }        

In your code D, you are overwriting the reference for allList.

And the backupItemList in CustomAdapter has the reference for the same. So if you reassign the reference for allList the changes will not be picked up in recycler view

first make sure allList is mutable

val allList = mutableListOf<MSetting>()

then in code D

allList.clear()
allList.addAll(SettingHandler().getListAllSetting())
mCustomAdapter.notifyDataSetChanged()