0

I am new in kotlin and retrofit, when I run this app it shows nothing in my device. But I check the logcat and found this error

No adapter attached; skipping layout

CountryActivity

var recyclerView: RecyclerView = findViewById(R.id.countryRecyclerView)
recyclerView.layoutManager = LinearLayoutManager(this)

var apiInterface: CountryDataInterface = CountryApiClient.getApiClient()!!.create(CountryDataInterface::class.java)
apiInterface.getCountryData().enqueue(object : Callback<List<Country>> {
   override fun onFailure(call: Call<List<Country>>, t: Throwable) {}

   override fun onResponse(call: Call<List<Country>>, response: Response<List<Country>>) {
   val countryData = response.body()!!
   recyclerView.adapter = CountryDataAdapter(countryData)
}

CountryDataAdapter

class CountryDataAdapter(var countryDataList: List<Country>?):
RecyclerView.Adapter<CountryDataAdapter.RecyclerViewHolder>() {
class RecyclerViewHolder(itemView: View):RecyclerView.ViewHolder(itemView) {
    var countryName: TextView = itemView.findViewById(R.id.countryName)
    var casesTotal: TextView = itemView.findViewById(R.id.casesTotal)
    var casesToday: TextView = itemView.findViewById(R.id.casesToday)
    var deathTotal: TextView = itemView.findViewById(R.id.deathTotal)
    var deathToday: TextView = itemView.findViewById(R.id.deathToday)
    var recoveredAll: TextView = itemView.findViewById(R.id.recoveredAll)
    var activeAll: TextView = itemView.findViewById(R.id.activeAll)
    var criticalAll: TextView = itemView.findViewById(R.id.criticalAll)

    fun bindData(countryDataList: List<Country>?, position: Int){
        countryName.text = countryDataList!!.get(position).countryName.toString()
        casesTotal.text = countryDataList!!.get(position).cases.toString()
        casesToday.text = countryDataList!!.get(position).todayCases.toString()
        deathTotal.text = countryDataList!!.get(position).deathTotal.toString()
        deathToday.text = countryDataList!!.get(position).deathToday.toString()
        recoveredAll.text = countryDataList!!.get(position).recovered.toString()
        activeAll.text = countryDataList!!.get(position).activePatient.toString()
        criticalAll.text = countryDataList!!.get(position).critical.toString()
    }
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {
    var view: View = LayoutInflater.from(parent!!.context).inflate(R.layout.country_row,parent,false)
    return RecyclerViewHolder(view)
}

override fun getItemCount(): Int {
    return countryDataList!!.size
}

override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
    holder.bindData(countryDataList,position)
}
}

How to resolve this error?

Aditya
  • 3,525
  • 1
  • 31
  • 38
user10997009
  • 142
  • 8

1 Answers1

0

Try to follow the approach:

  1. Create an empty adapter the same time you set LayoutManager for the RecyclerView. Save it as field of your View.
recyclerView.layoutManager = LinearLayoutManager(this)
countriesAdapter = CountryDataAdapter(ArraysList())
recyclerView.adapter = countriesAdapter
  1. When data is ready, populate the adapter and notify:
apiInterface.getCountryData().enqueue(object : Callback<List<Country>> {
        override fun onFailure(call: Call<List<Country>>, t: Throwable) {

        }

        override fun onResponse(call: Call<List<Country>>, response: Response<List<Country>>) {

            // reset data in adapter and not re-creating adapter:
            val countryData = response.body()!!
            countriesAdapter.setItems(countryData)
            runOnUiThread(Runnable {
                countriesAdapter.notifyDataSetChanged()
            })
       }
}
  1. In adapter:
class CountryDataAdapter(var countryDataList: MutableList<Country>):

 /// origin code

fun setItems(newItems: List<Country>) {
  this.items?.let {
    it.clear()
    it.addAll(newItems)
  }
}

}
S-Sh
  • 3,564
  • 3
  • 15
  • 19
  • i tried your code, but it's not work and show some error under red underline also maube you write java code countriesAdapter = new CountryDataAdapter(ArraysList()); – user10997009 Mar 23 '20 at 12:41
  • @user10997009, sorry, I really misused Java snippet. It fixed now ). As well, added some adapter changes according to the all the rest answer. Hope it'll help – S-Sh Mar 23 '20 at 14:52
  • still it's not working – user10997009 Mar 23 '20 at 17:04