使用适配器将数据绑定到RecyclerView的问题。没有看到模型类

问题描述 投票:0回答:4

基本上,我想使用适配器将数据绑定到我的RecyclerView,但似乎在定义适配器的Activity中看不到我的数据列表

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

似乎应该可以,但是可能存在一些误解或其他我无法解决的问题。

这是我的课程

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

可能是一些愚蠢的问题或误解,但我正在努力自己找到它。

android kotlin android-recyclerview adapter
4个回答
0
投票

不初始化

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

用途:

为适配器创建类变量

lateinit var formRecyclerAdapter: FormRecyclerAdapter

之后onCreate()做

formRecyclerAdapter = FormRecyclerAdapter(this, TestFileManager.testForms)

之后,将适配器初始化为回收站视图

listItems.adapter = formRecyclerAdapter

0
投票

首先,您没有初始化类,请执行以下操作:

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

第二,您使用错误的运算符将项目添加到ArrayList中,请使用add()。 set()不会将项目添加到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)
}

0
投票

尝试一下:

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)用指定的元素替换此列表中指定位置的元素。

您的列表为空。

Call:TestFileManager()。testForms


0
投票

所以我的主体现在看起来像这样:

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
    }

}

无法解决问题,因为:/仍然对.testForms不满意我看不到这节课的内容。不知道为什么

© www.soinside.com 2019 - 2024. All rights reserved.