使用 Adapter 将数据绑定到 RecyclerView 时出现问题。没有看到模型 class

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

基本上我想使用适配器将数据绑定到我的 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)
    }
}

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

试试这个:

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

您的列表是空的。

调用:TestFileManager().testForms

不要初始化

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

使用:

为适配器创建 class 变量

lateinit var formRecyclerAdapter: FormRecyclerAdapter

然后在 onCreate() 中执行

formRecyclerAdapter = FormRecyclerAdapter(this, TestFileManager.testForms)

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

listItems.adapter = formRecyclerAdapter

首先你没有初始化class,这样做:

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

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

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 的引用 我从这个 class 里面看不到任何东西。不知道为什么