1

I have created a fragment and inside that fragment I have a recyclerview but when my fragment is loaded nothing shows and it give me this error "E/RecyclerView: No adapter attached; skipping layout". Below is the code for the adapter and fragment class, any help would be appreciated

Adapter Class:

class ViewAllRecipeAdapter(private val newList: ArrayList<Recipes>) :
    RecyclerView.Adapter<ViewAllRecipeAdapter.MyViewHolder>() {


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.view_all_recipe_item, parent, false)

        return MyViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val currentItem = newList[position]
        holder.recipeName.text = currentItem.recipeName
        holder.recipeDesc.text = currentItem.recipeDescription
    }

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

    class MyViewHolder(itemview: View) : RecyclerView.ViewHolder(itemview) {

        val recipeName: TextView
        val recipeDesc: TextView

        init {
            recipeName = itemView.findViewById<View>(R.id.recipe_name) as TextView
            recipeDesc = itemView.findViewById<View>(R.id.recipe_description) as TextView
        }
    }
}

Fragment Class:

class ViewAllMyRecipesFragment : Fragment() {


    private lateinit var recyclerview: RecyclerView
    private lateinit var recipeData: ArrayList<Recipes>

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view: View = inflater.inflate(R.layout.fragment_view_all_my_recipes, container, false)

        recipeData = dummygenerator(10)

        recyclerview = view.findViewById<RecyclerView>(R.id.recycler_view_all_recipes)
        recyclerview.adapter = ViewAllRecipeAdapter(recipeData)
        recyclerview.layoutManager = LinearLayoutManager(view.context)
        recyclerview.setHasFixedSize(true)

        // Inflate the layout for this fragment
        return view
    }

    private fun dummygenerator(size: Int) : ArrayList<Recipes>{
        val list = ArrayList<Recipes>()

        for(i in 0 until size) {
            val drawable = when (i % 3) {
                0 -> "recipeName " +i
                1 -> "recipeDescription " + i
                else -> "Else " +i
            }

            val item = Recipes("title $i", "body")
            list += item
        }

        return list
    }

}

fragment_view_all_my_recipes.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".ui.recipe.ViewAllMyRecipesFragment">


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view_all_recipes"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:listitem="@layout/view_all_recipe_item" />

</androidx.constraintlayout.widget.ConstraintLayout>

view_all_recipe_item.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/recipe_image"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_marginEnd="331dp"
        android:layout_marginBottom="651dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:srcCompat="@tools:sample/avatars" />

    <TextView
        android:id="@+id/recipe_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="25dp"
        android:text="TextView"
        android:textStyle="bold"
        android:textSize="18sp"
        app:layout_constraintStart_toEndOf="@+id/recipe_image"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/recipe_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="10dp"
        android:text="TextView"
        app:layout_constraintStart_toEndOf="@+id/recipe_image"
        app:layout_constraintTop_toBottomOf="@+id/recipe_name" />


</androidx.constraintlayout.widget.ConstraintLayout>
Nadish
  • 89
  • 1
  • 1
  • 9
  • Does this answer your question? [recyclerview No adapter attached; skipping layout](https://stackoverflow.com/questions/29141729/recyclerview-no-adapter-attached-skipping-layout) – cutiko Nov 11 '21 at 10:29
  • That error doesn't usually matter, it's just letting you know the layout pass is happening before the adapter is attached. It should redisplay once you set the adapter. This definitely isn't a layout issue, right? A zero-height ``RecyclerView`` or zero-height list items? Have a check in the ``Layout Inspector`` window and see if the recycler is the right size, and if there are any items in its list hierarchy. You could also do some logging/debugging to check if ``onBindViewHolder`` is getting called – cactustictacs Nov 11 '21 at 14:04
  • onBindViewHolder is not getting called, I have added log in it but nothings prints. – Nadish Nov 11 '21 at 14:24
  • 1
    Is it possible you have two `RecyclerView`s in your layout by accident? – Ryan M Nov 11 '21 at 14:25
  • Alternately: are you sure that `onCreateView` is getting hit? Your code looks correct. – Ryan M Nov 11 '21 at 14:25
  • I have added xml files as well in this forum – Nadish Nov 11 '21 at 14:27
  • You've posted `FragmentLayout.xml` but you're inflating `R.layout.fragment_view_all_my_recipes` - could that be the issue? Otherwise, the XML files also look fine. – Ryan M Nov 11 '21 at 14:28
  • Yeah FragmentLayout.xml is just dummy name real name is fragment_view_all_my_recipes , I will edit that – Nadish Nov 11 '21 at 14:29
  • I'd recommend putting in a log to check if `onCreateView` is getting called. Your code looks correct to me (though I haven't run it); I suspect the problem is elsewhere. – Ryan M Nov 11 '21 at 14:30
  • I agree with you, even logging is not getting called from onCreateView. – Nadish Nov 11 '21 at 14:32
  • 1
    Your problem is elsewhere, then: this Fragment isn't getting added properly, and so that error log is coming from a different `RecyclerView`. – Ryan M Nov 11 '21 at 14:34
  • Thanks, you were right there were two recyclerview, now this problem has been solved. – Nadish Nov 11 '21 at 14:46

1 Answers1

0

Try moving all onCreateView logic to onViewCreated and

recyclerview.layoutManager = LinearLayoutManager(requireActivity())
recyclerview.adapter = ViewAllRecipeAdapter(recipeData)

because sometimes it causes problem when layoutManager is after adapter.

Anshul
  • 1,495
  • 9
  • 17