столкнулся с такой, проблемой надо передать ArrayList из объекта(там содержится только String) в Adapter. Код который у меня:
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
}
И функция где пытаюсь записать значение которые получил в 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()
}