I encountered such a problem, I need to transfer the ArrayList from the object (it contains only a String) to the Adapter. The code that I have is:

Adapter:

class AdapterC (val countryList: Collection<Countries>): RecyclerView.Adapter<AdapterC.ViewHolder>() { override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ViewHolder { val v = LayoutInflater.from(p0?.context).inflate(R.layout.country_list,p0,false) return ViewHolder(v) } override fun getItemCount(): Int { return countryList.size } override fun onBindViewHolder(p0: ViewHolder, p1: Int) { val country : Countries=countryList.toTypedArray()[p1] p0?.textViewName.text=country.name } class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){ val textViewName= itemView.findViewById(R.id.TextViewCountry) as TextView }} 

MainActivity:

 class MainActivity : AppCompatActivity() { val adapter= AdapterC(Collections.emptyList<Countries>()) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val recyclerView= findViewById(R.id.recyclerView) as RecyclerView recyclerView.layoutManager=LinearLayoutManager(this,LinearLayout.VERTICAL,false) recyclerView.adapter=adapter} 

And the function where I try to write the value that I received in the Adapter:

  private fun handleJson(jsonString:String?){ val jsonResult = JSONObject(jsonString) val resultobject=jsonResult.getJSONObject("results") val list =ArrayList<Countries>() var iter=resultobject.keys() iter.forEach { var country = resultobject.getJSONObject(it) var name = country.optString("name") var item=Countries(name) list.add(item) } // adapter.countryList.submitList(list) <-- Подсвечивает красным SubmitList // adapter.countryList.clear() <-- Подсвечивает красным clear // adapter.countryList.addAll(list) <-- Подсвечивает красным addList adapter.notifyDataSetChanged() } 

    1 answer 1

    Found a solution to the problem. In Adaptere, change the value of class AdapterC (val countryList: Collection<Countries>): RecyclerView.Adapter<AdapterC.ViewHolder>() { override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ViewHolder {

    On class AdapterC (val countryList: ArrayList<Countries>): RecyclerView.Adapter<AdapterC.ViewHolder>() { override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ViewHolder {

    In MainActivity, val adapter= AdapterC(Collections.emptyList<Countries>()) to val adapter= AdapterC(arrayListOf<Countries>())

    And then it will work.
    adapter.countryList.clear() adapter.countryList.addAll(list)