Search code examples
androidkotlinandroid-recyclerviewadapter

Problems with binding data to RecyclerView with Adapter. Not seeing model class


Basically I want to bind data to my RecyclerView using adapter but it seems like I don't see my data list in Activity where I define adapter

listItems.adapter = FormRecyclerAdapter(this, TestFileManager.testForms) // testForms highlighted(unresloved reference)

It seems like it should work but probably there is some misunderstanding or other problem I can't solve.

Here are my classes

MainActivity.kt

class MainActivity: AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(activity_main)
        val toolbar: Toolbar = findViewById(R.id.toolbar)
        setSupportActionBar(toolbar)
        supportActionBar?.title = "Forms creator"


        listItems.layoutManager = LinearLayoutManager(this)
        listItems.adapter = FormRecyclerAdapter(this, TestFileManager.testForms) <- here is where the problem occurs
    }

}

TestFileManager.kt

class TestFileManager {

    private val testForms = ArrayList<TestForm>()

    init {
        initializeForms()
    }

    fun initializeForms() {
        var testForm = TestForm("Form 1", "Created Jun 1 2020")
        testForms.set(0, testForm)

        testForm = TestForm("Form 2", "Created 5 Jan 2019")
        testForms.set(1, testForm)

        testForm = TestForm("Form 3", "Created 3 March 2020")
        testForms.set(2, testForm)
    }
}

TestForm.kt

class TestForm( var name: String, var description: String)

FormRecyclerAdapter.kt

class FormRecyclerAdapter(private val context: Context, private val forms: List<TestForm>) :
    RecyclerView.Adapter<FormRecyclerAdapter.ViewHolder>() {

    private val layoutInflater = LayoutInflater.from(context)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val itemView = layoutInflater.inflate(R.layout.item_form_list, parent, false)
        return ViewHolder(itemView)
    }

    override fun getItemCount() = forms.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val form = forms[position]
        holder.textTitle?.text = form.name
        holder.textDescription?.text = form.description
    }

    class ViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView!!) {
        val textTitle = itemView?.findViewById<TextView?>(R.id.textTitle)
        val textDescription = itemView?.findViewById<TextView?>(R.id.textDescription)
    }
}

Probably some silly issue or misunderstanding but I'm struggling to find it myself.


Solution

  • First of all you didn't initialize the class, do this:

    listItems.adapter = FormRecyclerAdapter(this, TestFileManager().testForms)
    

    Secondly you are using wrong operator to add items into ArrayList, use add(). set() won't add the items into the ArrayList.

    fun initializeForms() {
        var testForm = TestForm("Form 1", "Created Jun 1 2020")
        testForms.add(testForm)
    
        testForm = TestForm("Form 2", "Created 5 Jan 2019")
        testForms.add(testForm)
    
        testForm = TestForm("Form 3", "Created 3 March 2020")
        testForms.add(testForm)
    }