1

When attempting to use Kotlin Extensions within a RecyclerView.Adapter to refer to Views in an XML layout file, Intellij cannot recognize the Views by their IDs, even with seemingly correct imports. The odd thing about this is that it will work inside of an Activity or Fragment. Is there additional context needed for this to work in a RecyclerView.Adapter?

Intellisense not picking up Views in a RecyclerView.Adapter implementation: Intellisense not picking up Views in a RecyclerView.Adapter implementation

View was found with practically the same imports View was found with practically the same imports

And here is my XML, just in case

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp"
    >

    <LinearLayout
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_list_repo_name"
            android:text="@string/repo_name"
            android:textStyle="bold"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/tv_item_repo_description"
            android:text="@string/description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="center_vertical">

        <TextView
            android:id="@+id/tv_item_forks"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:drawableStart="@drawable/ic_fork"
            android:text="10"
            android:gravity="end"/>

        <TextView
            android:id="@+id/tv_item_stars"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:drawableStart="@drawable/ic_star"
            android:gravity="end"
            android:text="14,000"
            />


    </LinearLayout>


</LinearLayout>
Josh Ribeiro
  • 1,349
  • 3
  • 18
  • 28
  • did you look at this [question](https://stackoverflow.com/q/33304570/9119277) ? – Napster Jan 23 '18 at 18:26
  • Yes, thanks for asking just in case. I have an identical import scenario and have even tried importing both kotlinx.....yourview.* and kotlinx....yourview.view.* – Josh Ribeiro Jan 23 '18 at 18:34
  • It won't work directly in your `getItemCount` method. To access that, you need to use the view in your viewHolder class. – karandeep singh Jan 23 '18 at 18:51
  • After considering your point, the same issue holds true when attempted within the custom Viewholder, which accepts a View parameter. This also holds true on onCreateViewHolder within the adapter class – Josh Ribeiro Jan 23 '18 at 19:08

1 Answers1

1

It seems you're trying to get the view outside the onBindViewHolder.

Please take a look at this sample:

import kotlinx.android.synthetic.main.row_badges_item.view.*

class BadgesAdapter(private val mActivity: Activity) : RecyclerView.Adapter<BadgesAdapter.ViewHolder>() {

override fun onBindViewHolder(holder: ViewHolder, position: Int) {

    holder.bind(position)
}

override fun getItemCount(): Int {
     ba // Error here
    return mData.size
}

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

    fun bind(position: Int) {

        with(itemView) {

            val badge = mData[position] 

            badges_title.text = badge.badgeTitle // Fine here                
        }

    }

}

}

Hope that helps. Thanks. :)

Francislainy Campos
  • 3,462
  • 4
  • 33
  • 81