Search code examples
androidandroid-recyclerviewgrid-layout

got E/No adapter attached; skipping layout even when I attach a Layoutmanager and the adapter


I try to create a recyclerview for my gallery photo and video, I'm sur the data is correctly collected, an adapter is created and attached to my recycler view before the error.

I created the recyclerview as follow :

fileList = FileUtils.filesInGaleryFolder
itemAdapter = GalerieAdapter(this, fileList)
galeriebind.recyclerGallery.layoutManager = GridLayoutManager(this, 3)
galeriebind.recyclerGallery.adapter = itemAdapter

Here is my adapter class:


class GalerieAdapter(ctx: Activity, items: List<File>) : RecyclerView.Adapter<GalerieAdapter.ViewHolder>() {
    //--- VARIABLES -----//
    var ctx: Context = ctx
    var items: List<File> = items
    var esVideo = false


    /*****
     * This method is called right when the adapter is created and is used to initialize your ViewHolder(s).
     *****/
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.gallery_item, parent, false)
        return ViewHolder(view)
    }

Below you will also find my xml for the activity and the adapter:

<?xml version="1.0" encoding="utf-8"?>


<androidx.core.widget.NestedScrollView
    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="match_parent"
    android:fillViewport="true"
    tools:context=".Galerie">

    <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="match_parent"
        android:background="@drawable/bg_home"
        tools:context=".Galerie">

        <ImageView
            android:id="@+id/imageTitle"
            android:layout_width="230dp"
            android:layout_height="24dp"
            android:layout_marginStart="24dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/logo_home" />

        <View
            android:id="@+id/view"
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_marginTop="15dp"
            android:background="@color/detailsgrey"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageTitle" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycler_gallery"
            app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginStart="24dp"
            android:layout_marginEnd="24dp"
            android:layout_marginTop="55dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="@+id/view"
            app:layout_constraintStart_toStartOf="@+id/view"
            app:layout_constraintTop_toBottomOf="@+id/view" />


        <ImageButton
            android:id="@+id/backbutton"
            android:layout_width="36dp"
            android:layout_height="36dp"
            android:layout_marginStart="24dp"
            android:layout_marginTop="5dp"
            android:background="@color/transparent"
            android:padding="6dp"
            android:scaleType="fitXY"
            android:src="@drawable/ic_arrow_left"
            android:onClick="onClick"
            app:layout_constraintBottom_toBottomOf="@+id/imageTitle"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/imageTitle" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>

and the one for the adapter:

<?xml version="1.0" encoding="utf-8"?>
    
    
    <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:id="@+id/container"
        android:layout_width="178dp"
        android:layout_height="120dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/bt_galery_item_selector">
    
    
        <TextView
            android:id="@+id/date"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:layout_marginEnd="16dp"
            android:fontFamily="@font/nunito_bold"
            android:textColor="@color/BLACK"
            app:layout_constraintBottom_toTopOf="@+id/hour"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.505"
            app:layout_constraintStart_toStartOf="parent"
            tools:text="12/12/2019" />
    
        <TextView
            android:id="@+id/hour"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:layout_marginEnd="16dp"
            android:layout_marginBottom="24dp"
            android:textSize="12sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.505"
            app:layout_constraintStart_toStartOf="parent"
            tools:text="20:45:03" />
    
        <View
            android:id="@+id/bg_categoria"
            android:layout_width="0dp"
            android:layout_height="24dp"
            android:layout_marginStart="20dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="48dp"
            android:background="@drawable/rounded_shape"
            android:backgroundTint="@color/greenbutton"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <TextView
            android:id="@+id/categoria"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginEnd="20dp"
            android:fontFamily="@font/nunito_bold"
            android:gravity="start|right|left|end"
            android:textColor="@color/WHITE"
            android:textSize="12sp"
    
            app:layout_constraintBottom_toBottomOf="@+id/bg_categoria"
            app:layout_constraintEnd_toEndOf="@+id/bg_categoria"
            app:layout_constraintStart_toEndOf="@+id/imgCategoria"
            app:layout_constraintTop_toTopOf="@+id/bg_categoria"
            tools:text="VIDEO" />
    
    
        <ImageView
            android:id="@+id/imgCategoria"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginStart="10dp"
            android:layout_marginTop="2dp"
            android:layout_marginBottom="2dp"
            app:layout_constraintBottom_toBottomOf="@+id/bg_categoria"
            app:layout_constraintStart_toStartOf="@+id/bg_categoria"
            app:layout_constraintTop_toTopOf="@+id/bg_categoria"
            app:srcCompat="@drawable/ic_video_label" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>

I saw that the problem can be related to the lack of grid layout, but I initialized one and passed it to the recyclerview, and I still get the error "No adapter attached; skipping layout" and no item are displayed. I also found it can be related to asynchronous call, but i don't think there is any asynchronous in my case as it's in the OnCreate(). and event with the notifyDataSetChanged() but it doesn't solve the problem.

I also try to initialize the adapter first then init data as following:

itemAdapter = GalerieAdapter(this, fileList)
galeriebind.recyclerGallery.adapter = itemAdapter
fileList = FileUtils.filesInGaleryFolder
itemAdapter!!.setFiles(fileList)

I checked the data in galeriebind.recyclerGallery.adapter is correctly updated and i call the notifyDataSetChanged() in the setFiles() method


Solution

  • I finally found out the answer, the problem is coming from my binding, I just replaced :

    itemAdapter = GalerieAdapter(this, fileList)
    galeriebind.recyclerGallery.adapter = itemAdapter
    fileList = FileUtils.filesInGaleryFolder
    itemAdapter!!.setFiles(fileList)
    

    by:

    itemAdapter = GalerieAdapter(Activity(), fileList)
    var recyclerview = findViewById<RecyclerView>(R.id.recycler_gallery)
    recyclerview.adapter = itemAdapter
    fileList = FileUtils.filesInGaleryFolder
    itemAdapter.setFiles(fileList)
    val glm = GridLayoutManager(this, 3)
    glm.orientation = GridLayoutManager.HORIZONTAL
    recyclerview.layoutManager = glm
    

    Using the findviewbyID solved it