CoderWithAProblem
CoderWithAProblem

Reputation: 11

Unresolved reference: RecyclerView when trying to inherit Adapter

I am trying to make a RecyclerView in Android Studio and I am struggling to inherit from RecyclerView.Adapter<>()

This is the fragment of code that contains the problem:

package Adapters

import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.R
import android.os.Bundle
import models.Post

class PostAdapter(private val postList: List<Post>, private val content: String) : RecyclerView.Adapter<PostAdapter.PostViewHolder>() {

Android Studio sets "RecyclerView" red and I can't get rid of it. I tried solutions from other posts on stackoverflow, tried to follow tutorials on the internet, but there isn't anything about androidx and I can't use older version (android.support.v7.widget.RecyclerView), because Android Studio won't let me. I imported RecyclerView in mains build.gradle:

implementation "androidx.recyclerview:recyclerview:1.1.0"

And added google() in the highest-instance build.gradle I hope I explained the problem properly and you can help me.

Upvotes: 1

Views: 2167

Answers (3)

In my case, I was using the wrong library version. My library version was way too greater than the released version.

implementation 'androidx.recyclerview:recyclerview:2.9.0'

implementation 'androidx.recyclerview:recyclerview:1.2.1'

Upvotes: 0

paida
paida

Reputation: 1

SOLVED! Did you remember to specify an id for the recyclerview after adding it to your main activity? If yes, then check to make sure that the id you gave is exactly the same as the red text that's causing an unresolved error. Eg

If in main_activity.xml you have

   `<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="409dp"
        android:layout_height="729dp"
        tools:layout_editor_absoluteX="1dp"
        tools:layout_editor_absoluteY="1dp" />`

Then in MainActivity.kt you should have

`recyclerView.layoutManager = StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL)`

The word "recyclerView" should be exactly the same in the 2 files where it is used

Upvotes: 0

Dmitrii Leonov
Dmitrii Leonov

Reputation: 1389

I think you are missing import of recycler view

import androidx.recyclerview.widget.RecyclerView

Also If you've just migrated to Android X Android studio might be buggy so you want to do File -> Invalidate Caches/Restart

Upvotes: 1

Related Questions