0

I got some problems in my program, when it runs the recycleView not working and it say "No adapter attached; skipping layout", i have no clue to fix the error

heres the code

Main Activity




override fun onProductsLoadSuccess(productsModelList: List<Products>?) {
    val adapter = ProductsAdapter(this, productsModelList!!)
    rv_produk.adapter = adapter
    rv_produk.addItemDecoration(SpaceItemDecoration())
}








private fun init() {
    productsLoadListener = this
    val gridLayoutManager = GridLayoutManager(this,1)
    rv_produk.layoutManager = gridLayoutManager
}



ProductAdapter





class ProductsAdapter(
    private val context: Context,
    private val list: List<Products>
): RecyclerView.Adapter<ProductsAdapter.ProductsViewHolder>() {

    class ProductsViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var gambarProduk: ImageView? = null
        var namaProduk: TextView? = null
        var hargaProduk: TextView? = null

        init {
            gambarProduk = itemView.findViewById(R.id.gambar_produk) as ImageView
            namaProduk = itemView.findViewById(R.id.nama_produk) as TextView
            hargaProduk = itemView.findViewById(R.id.harga_produk) as TextView
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductsViewHolder {
        return ProductsViewHolder(LayoutInflater.from(context)
            .inflate(R.layout.item_product,parent,false))
    }

    override fun onBindViewHolder(holder: ProductsViewHolder, position: Int) {
        Glide.with(context)
            .load(list[position].image)
            .into(holder.gambarProduk!!)
        holder.namaProduk!!.text = StringBuilder().append(list[position].name)
        holder.hargaProduk!!.text = StringBuilder("Rp").append(list[position].price)
    }

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

}




Please Help me

I wanna build a recycler view with realtime database on firebase

haainips
  • 11
  • 3

1 Answers1

0

Make sure you are initializing your recycler view on the main thread and not in any asynchronous callback from firebase and instance it as soon as the view is instantiated (in your onCreate() method).

If you have no data when recycler view is instantiated just pass an empty list and update the adapter whenever new information is available.

See this post, answered by Peter Jun 1 recyclerview No adapter attached; skipping layout

Chnoou
  • 23
  • 5