Joe Shamuraq
Joe Shamuraq

Reputation: 1385

Kotlin: Passing data/array from Recyclerview Activity to Adapter

I am trying to pass an array from my Recyclerview Activity to its Adapter as such:

    //Setting NavBar Title
    val navBarTitle = intent.getStringExtra(FirstCustomViewHolder.LESSON_TITLE_KEY)
    supportActionBar?.title = navBarTitle

    var content : Array<String>
    if (navBarTitle == "Introduction"){
        content = arrayOf("Intro1", "Intro2")
    }
    else{
        content = arrayOf(":esson1-1", "Lesson 1-2")
    }

I am passing the array as such:

    recyclerView_main.adapter = SecondAdapter(content)

And I am getting an angry red underline as shown below. @the var content

On mouse-over the pop-up error reads: Too many arguments for public constructor...... Is there a proper way to pass an array or variable to my adapter? I am fairly new to Kotlin and appreciate and pointers.

Thank you.

Edit: As requested, this is my adapter class:

class SecondAdapter : RecyclerView.Adapter<SecondCustomViewGolder>(){


    //Variable below to be replaced by array from Activity
    var lessons = arrayOf("Satu", "Dua", "Tiga", "Empat", "Lima", "Enam", "Tujuh",
        "Lapan", "Sembilan")

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

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SecondCustomViewGolder {
        var layoutInflater = LayoutInflater.from(parent.context)
        var cellForRow = layoutInflater.inflate(R.layout.lesson_row, parent, false)
        return SecondCustomViewGolder(cellForRow)
    }

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

    }
}
class SecondCustomViewGolder(var viewTwo : View) : RecyclerView.ViewHolder(viewTwo){

}

Upvotes: 0

Views: 2198

Answers (2)

Mateus Pereira
Mateus Pereira

Reputation: 36

Does your SecondAdapter class constructor accept an Array as an argument? If not, you must add it there. The error is because you're trying to pass an argument to a constructor that accepts no arguments.

EDIT

Do it like so:

class SecondAdapter(val lessonArray: Array<String>) : RecyclerView.Adapter<SecondCustomViewGolder>(){

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

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SecondCustomViewGolder {
        var layoutInflater = LayoutInflater.from(parent.context)
        var cellForRow = layoutInflater.inflate(R.layout.lesson_row, parent, false)
        return SecondCustomViewGolder(cellForRow)
    }

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

    }
}
class SecondCustomViewGolder(var viewTwo : View) : RecyclerView.ViewHolder(viewTwo){

}

I made it a val since it's my preference. If you intend to modify the variable, than you just declare it as a var in the constructor. There's no need to assign it inside the class. Just declaring it in the constructor makes it accessible throughout the class.

Upvotes: 1

Shripad Jadhav
Shripad Jadhav

Reputation: 316

You can use the ListAdapter and use submitList()

Upvotes: 0

Related Questions