Alky
Alky

Reputation: 67

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.

Upvotes: 1

Views: 88

Answers (4)

Alky
Alky

Reputation: 67

So my main now looks like this:

class MainActivity: AppCompatActivity() {

    lateinit var formRecyclerAdapter: FormRecyclerAdapter

    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)
        formRecyclerAdapter = FormRecyclerAdapter(this, TestFileManager.testForms)
        listItems.adapter = formRecyclerAdapter
    }

}

Didn't solve the problem tho :/ Still unresloved refrence on .testForms I cant see anything from instide this class. not sure why

Upvotes: 0

Kasım &#214;zdemir
Kasım &#214;zdemir

Reputation: 5634

Try this :

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)
}

ArrayList.set(int index, E element) replaces the element at the specified position in this list with the specified element.

Your list is empty.

Call : TestFileManager().testForms

Upvotes: 1

Animesh Sahu
Animesh Sahu

Reputation: 8096

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)
}

Upvotes: 0

suhas sasuke
suhas sasuke

Reputation: 710

dont initialize

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

use:

create class variable for adaptor

lateinit var formRecyclerAdapter: FormRecyclerAdapter

after that in onCreate() do

formRecyclerAdapter = FormRecyclerAdapter(this, TestFileManager.testForms)

after that initialize your adaptor to your recycler view

listItems.adapter = formRecyclerAdapter

Upvotes: 0

Related Questions