Problem with recyclerView (Kotlin)

0

At the moment of creating an adapter for a recyclerView I could not make it recognize the layout that happened to it and the elements that are in it.

class adapter_wpHome(val mItems: List<content_wallp>, val context : Context) : RecyclerView.Adapter<adapter_wpHome.viewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): viewHolder {
        return viewHolder(LayoutInflater.from(parent.context).inflate(R.layout.cv_list_wall, parent, false))
    }

    override fun getItemCount(): Int = mItems.size

    override fun onBindViewHolder(holder: viewHolder, position: Int) {
        holder.bindItems(mItems[position])
    }

    class viewHolder( itemView: View) : RecyclerView.ViewHolder(itemView) {


        fun bindItems ( item : content_wallp){

            Glide.with(itemView.context).load(item.path).into(itemView.img_wallp) // aqui no encuentra el elemento img_wallp en el layout

        }

    }
}

It is worth noting that I have 3 more applications where I have done the adapter in the same way and everything works perfectly, I do not know why it has failed, I have already reviewed all the other adapters (my other apps) and they are the same! I do not know what's wrong, the app was all in java and I'm passing it to Kotlin, in this class I used the android studio option that says "From java to Kotlin", the adapter I'm creating in the same class that I'm going to use, the data class equally and it is not an activity, it is a fragment, thanks for the collaboration.

This is the error that shows in console

    
asked by Leonardo Henao 23.01.2018 в 05:13
source

2 answers

1

Thanks to everyone for the collaboration, I publish my solution in this way (in response) since it can be useful for some other user who has this same problem ...

After reviewing my projects from "head to toe" which I have already passed to Kotlin and which work perfectly, I found that in the build gradle of the module app after the dependencies in all were added

apply plugin: 'com.google.gms.google-services'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

and for some reason that I do not know about in this project (the one that gave me the error) the two last lines had not been added automatically, I added them and everything worked perfectly ... So for me, solve this error adding the following lines to the build gradle

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

After

dependencies {
    ...
    ...
}
    
answered by 24.01.2018 в 06:07
0

This error has happened to me a few times, and the solution was found here With solution 1 it was that I corrected the problem.

Go to File -> Settings -> Compiler and add the flags of --stacktrace --debug to the compiler commands. This happened to me because somewhere I had an error in the code, but for some reason kotlin does not tell me where ¯ \ _ (ツ) _ / ¯. When adding the flags, you will have to read the log a bit and hopefully you will find where the bug is

Another option is to declare the variables at the top along with the constructor, something like this

class adapter_wpHome(val mItems: List<content_wallp>, val context : Context) : RecyclerView.Adapter<adapter_wpHome.viewHolder>() {
    val img_wallp = view.img_wallp
    //etc etc

and in bindItems make use of img_wallp

    
answered by 23.01.2018 в 23:51