android / views-widgets-samples

Multiple samples showing the best practices in views-widgets on Android.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

E/RecyclerView: No adapter attached; skipping layout

Umesh-01 opened this issue · comments

Description

I am inflating some data in Favorites Fragment (RecyclerView) with the help of Favorites Recycler Adapter and when I am running my app it is not showing anything in the Favorites Fragment even the app is launching without any exceptions but when I press back button to navigate back to the previous fragment from the current fragment (i.e. Favorites Fragment) then I am getting this error (E/RecyclerView: No adapter attached; skipping layout) in logcat window of Android Studio.

My Code for Favorites Fragment

class FavouritesFragment : Fragment() {

    lateinit var recyclerFavourite: RecyclerView
    lateinit var progressLayout: RelativeLayout
    lateinit var progressBar: ProgressBar
    lateinit var layoutManager: RecyclerView.LayoutManager
    lateinit var recyclerAdapter: FavouriteRecyclerAdapter
    var dbBookList = listOf<BookEntity>()


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment

        val view = inflater.inflate(R.layout.fragment_favourites, container, false)

        recyclerFavourite = view.findViewById(R.id.RecyclerFavourite)

        progressLayout = view.findViewById(R.id.ProgressLayout)

        progressBar = view.findViewById(R.id.ProgressBar)

        progressLayout.visibility = View.VISIBLE

        layoutManager = GridLayoutManager(activity, 2)

        dbBookList = RetrieveFavourites(activity as Context).execute().get()

        if (activity != null) {

            progressLayout.visibility = View.GONE

            recyclerAdapter = FavouriteRecyclerAdapter(activity as Context, dbBookList)

            recyclerFavourite.adapter = recyclerAdapter

            recyclerFavourite.layoutManager = layoutManager

        }

        return view
    }

    class RetrieveFavourites(val context: Context) : AsyncTask<Void, Void, List<BookEntity>>() {
        override fun doInBackground(vararg p0: Void?): List<BookEntity> {

            val db = Room.databaseBuilder(context, BookDatabase::class.java, "book-db").build()

            return db.bookDao().getAllBooks()
        }

    }

}

My code for Favorites Recycler Adapter

class FavouriteRecyclerAdapter(val context: Context, var bookList: List<BookEntity>) :
    RecyclerView.Adapter<FavouriteRecyclerAdapter.FavouriteViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FavouriteRecyclerAdapter.FavouriteViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.recycler_favorite_single_row, parent, false)

        return FavouriteViewHolder(view)
    }

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

    override fun onBindViewHolder(holder: FavouriteRecyclerAdapter.FavouriteViewHolder, position: Int) {

        val book = bookList[position]

        holder.txtBookName.text = book.bookName
        holder.txtBookAuthor.text = book.bookAuthor
        holder.txtBookPrice.text = book.bookPrice
        holder.txtBookRating.text = book.bookRating
        Picasso.get().load(book.bookImage).error(R.drawable.default_book_cover)
            .into(holder.imgBookImage)

        notifyDataSetChanged()

    }

    class FavouriteViewHolder(view: View) : RecyclerView.ViewHolder(view) {

        val txtBookName: TextView = view.findViewById(R.id.txtFavBookTitle)
        val txtBookAuthor: TextView = view.findViewById(R.id.txtFavBookAuthor)
        val txtBookPrice: TextView = view.findViewById(R.id.txtFavBookPrice)
        val txtBookRating: TextView = view.findViewById(R.id.txtFavBookRating)
        val imgBookImage: ImageView = view.findViewById(R.id.imgFavBookImage)
        val llContext: LinearLayout = view.findViewById(R.id.llFavContent)

    }

}

Screenshot

errors in book mart app