Saya baru di kotlin dan retrofit, ketika saya menjalankan aplikasi ini tidak menunjukkan apa-apa di perangkat saya. Tetapi saya memeriksa logcat dan menemukan kesalahan iniNo adapter attached; skipping layout. Beberapa waktu yang lalu saya memposting pertanyaan ini, beberapa orang menjawab ini tetapi jawaban ini tidak benar

Aktivitas Negara.kt

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.kt

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)
}
}
0
user10997009 23 Maret 2020, 13:19

1 menjawab

Jawaban Terbaik

Saya pikir masalah Anda adalah karena Anda memanggil recyclerView.adapter = CountryDataAdapter(countryData) di fungsi Async Anda.

Dari apa yang ditampilkan postingan Anda

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

recyclerView.adapter = CountryDataAdapter(countryData) ada di onResponse yang asinkron, saat aktivitas Anda makan siang, adaptor Anda tidak disetel karena menunggu hasil dari jaringan untuk menyetelnya.

Akan lebih baik untuk mengaturnya ke:

  1. Di awal aktivitas Anda, tambahkan var countryData: List<Country> = ArrayList()

EDIT setelah memeriksa Repo:

Anda tidak mendapatkan apa-apa karena panggilan Anda masuk ke onFailure. Anda harus menambahkan log untuk melihat hal-hal semacam ini.

Aktivitas Negara:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_country)

        val recyclerView: RecyclerView = findViewById(R.id.cRecyclerView)
        var countryData: List<Country> = ArrayList()
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.setHasFixedSize(true)
        recyclerView.adapter = CountryDataAdapter(countryData)
       // recyclerView.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))

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

                val data = Country(1, 2,3,4,5,6,7,8)
                countryData = listOf(data)
                recyclerView.adapter = CountryDataAdapter(countryData)

                Log.d("onFailure", "ERROR")
            }

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

        })

    }

Adaptor:

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

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

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {
        val 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)
    }
}
1
Biscuit 23 Maret 2020, 17:42