我是Kotlin和翻新的新手,当我运行此应用程序时,它在我的设备上什么都没有显示。但是我检查了logcat并发现了这个错误No adapter attached; skipping layout
。有时我发布了这个问题,有人回答了这个问题,但是这个回答不正确
CountryActivity.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 :(得分:1)
我认为您的问题是因为您正在异步函数中调用recyclerView.adapter = CountryDataAdapter(countryData)
。
您的信息显示
override fun onResponse(call: Call<List<Country>>, response: Response<List<Country>>) {
val countryData = response.body()!!
recyclerView.adapter = CountryDataAdapter(countryData)
recyclerView.adapter = CountryDataAdapter(countryData)
位于onResponse
中,它是异步的,当您进行活动午餐时,未设置适配器,因为它等待网络中的结果进行设置。
最好将其设置为:
var countryData: List<Country> = ArrayList()
在检查回购后进行编辑:
您什么也没收到,因为您的通话打入了onFailure
。您应该添加日志以查看这种情况。
CountryActivity:
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)
}
}
})
}
适配器:
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)
}
}