android - 我面临错误“未连接适配器;跳过布局”
问题描述
我是 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)
}
}
解决方案
我认为您的问题是因为您正在调用recyclerView.adapter = CountryDataAdapter(countryData)
Async 函数。
根据您的帖子显示
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
。您应该添加日志以查看此类内容。
国家活动:
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)
}
}
推荐阅读
- reporting-services - 如何在SSRS的折线图中设置最大横轴范围?
- python - 尽管分配了内存,但多处理池仍无法正常工作
- java - 深度复制模板类
- ios - JSONDecoder 无法解码 Array 但可以解码 String
- javascript - Vue.js 跨组件共享输入数据
- python - 如何让 Mypy 在 Callable 中识别类的协议成员资格?
- powershell - 为特定页面组合 pdftk 字符串
- .net - 对 .Net 4.0 的 TLS 1.3 支持
- python - kivy:如何从 FileChooserListView 中的所有项目中删除突出显示
- asp.net-core - masstransit 与 asp.net core 3.1 的集成