[英]RecycleView - No adapter attached, skipping layout in Fragment
Sup guys,伙计们,
I am currently implementing the ListAdapter for my RecycleView.我目前正在为我的 RecycleView 实现 ListAdapter。 While implementing I followed this tutorial: https://medium.com/simform-engineering/listadapter-a-recyclerview-adapter-extension-5359d13bd879
在实施时,我遵循了本教程: https://medium.com/simform-engineering/listadapter-a-recyclerview-adapter-extension-5359d13bd879
There is a problem with which I am currently struggeling with: Code:我目前正在努力解决一个问题:代码:
adapter = UsersAdapter()
rvUsers.adapter = adapter
userListLiveData.observe(this, Observer {list->
adapter.submitList(list)
})
I dont know if I implemented this correctly, because I get the error that the adapter isnt attached.我不知道我是否正确实现了这一点,因为我收到适配器未连接的错误。
Here`s my Code:这是我的代码:
class InvoiceListAdapter : ListAdapter<InvoiceModel, InvoiceViewHolder>(InvoiceListDiffCallback()) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): InvoiceViewHolder {
return InvoiceViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.fragment_first, parent, false))
}
override fun onBindViewHolder(holder: InvoiceViewHolder, position: Int) {
holder.bindTo(position)
}
override fun getItemCount(): Int {
val count = super.getItemCount()
return when(count) {
0 -> 1
else -> count
}
}
}
class InvoiceListDiffCallback : DiffUtil.ItemCallback<InvoiceModel>() {
override fun areItemsTheSame(oldItem: InvoiceModel, newItem: InvoiceModel): Boolean {
return oldItem == newItem
}
override fun areContentsTheSame(oldItem: InvoiceModel, newItem: InvoiceModel): Boolean {
return oldItem == newItem
}
}
My Fragment:我的片段:
class FirstFragment : Fragment() {
private lateinit var invoiceAdapter: InvoiceListAdapter
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_first, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val items = mutableListOf<InvoiceModel>()
val layoutManager = LinearLayoutManager(context)
invoiceAdapter = InvoiceListAdapter()
invoiceList.layoutManager = layoutManager
invoiceList.adapter = invoiceAdapter
for (i in 0..40) {
print(i)
}
items.add(
InvoiceModel(
"Einkauf",
"Digital",
"27.05.2020 12:00 Uhr",
"Wasser gekauft",
null,
"Portmonaiee",
true
)
)
invoiceAdapter.submitList(items)
}
}
Has anyone an idea how to fix that the adapter isnt attached?
Well you should notify the adapter about the changes in your list.好吧,您应该将列表中的更改通知适配器。 That way they will be shown on the display.
这样它们就会显示在显示屏上。 You could try with: invoiceAdapter.notifydatasetChanged()
您可以尝试: invoiceAdapter.notifydatasetChanged()
Or this one: invoiceAdapter.notifyItemInserted(items.size - 1)或者这个:invoiceAdapter.notifyItemInserted(items.size - 1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.