RecyclerView:没有适配器连接;跳过布局 kotlin

2022-01-28 14:07:15 标签 androidkotlin

我知道这个问题已经被张贴,但似乎所有张贴在java代码。我只是想实现一个小的例子recyclerView。我不知道我做错了什么任何见解都是很好的TIA。

MainActivity。kt

package com.wildcardev.androidtest1
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import com.wildcardev.androidtest1.adapters.RecyclerAdapter
import com.wildcardev.androidtest1.databinding.ActivityMainBinding
import com.wildcardev.androidtest1.models.TestDataObj
fun addDataToList(arrayList:ArrayList<TestDataObj>): ArrayList<TestDataObj>{
    arrayList.add(TestDataObj("tes1", "test2", "test3"))
    arrayList.add(TestDataObj("test2", "testt2", "testt2"))
    arrayList.add(TestDataObj("tes12", "test22", "test32"))
    arrayList.add(TestDataObj("tes13", "test23", "test33"))
    arrayList.add(TestDataObj("tes14", "test24", "test34"))
    arrayList.add(TestDataObj("tes15", "test25", "test35"))
    return arrayList
}
class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
     val testList: ArrayList<TestDataObj> = ArrayList()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(R.layout.activity_main)
        val list = addDataToList(testList)
        Log.d("TEST1","$list")
        Log.d("TEST1","${binding.recyclerView}")
        binding.recyclerView.layoutManager = LinearLayoutManager(this@MainActivity)
        val adapter = RecyclerAdapter(list)
        binding.recyclerView.adapter = adapter
    }
}

TestDataObj。kt

包com。wildcardev。androidtest1。models

data class TestDataObj(
   val title: String,
   val description: String,
   val date: String
)

RecyclerAdapter。kt

包com。wildcardev。androidtest1。adapters

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.wildcardev.androidtest1.R
import com.wildcardev.androidtest1.models.TestDataObj
class RecyclerViewHolder(view: View): RecyclerView.ViewHolder(view){
    var title: TextView = view.findViewById(R.id.title)
    var description: TextView = view.findViewById(R.id.description)
    var date: TextView = view.findViewById(R.id.date)
}
class RecyclerAdapter(private var list: ArrayList<TestDataObj>): RecyclerView.Adapter<RecyclerViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {
        Log.d("onCreateView", "INHOLDERcreate")
        val view = LayoutInflater.from(parent.context).inflate(R.layout.activity_main, parent, false)
        return RecyclerViewHolder(view)
    }
    override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
        Log.d("ADAPTER", "INBINDfunc")
        val item = list[position]
        holder.title.text = item.title
        holder.description.text = item.description
        holder.date.text = item.date
    }
    override fun getItemCount(): Int {
        Log.d("COUNT",".getItemCountcalled")
        return list.size
    }
} 

# # #试

绑定= ActivityMainBinding。inflate (layoutInflater)

setContentView (binding。root)

###我解决了我的问题。当使用viewBinding/dataBinding时,你需要将dataBinding传递给viewHolder。不幸的是,我没有得到这一点,因为我只是遵循教程,使用合成导入。因此,我将适配器代码更改为以下内容

RecyclerAdapter。kt

    package com.wildcardev.androidtest1.adapters
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.wildcardev.androidtest1.models.TestDataObj
import com.wildcardev.androidtest1.databinding.ListItemBinding
class RecyclerViewHolder(viewDataBinding: ListItemBinding): RecyclerView.ViewHolder(viewDataBinding.root){
    var title: TextView? = viewDataBinding.title
    var description: TextView? = viewDataBinding.description
    var date: TextView? = viewDataBinding.date
}
class RecyclerAdapter(private var list: ArrayList<TestDataObj>): RecyclerView.Adapter<RecyclerViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {
        Log.d("onCreateView", "INHOLDERcreate")
        val binding = ListItemBinding.inflate(LayoutInflater.from(parent.context),parent,false)
        return RecyclerViewHolder(binding)
    }
    override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
        Log.d("ADAPTER", "INBINDfunc $holder")
        val item = list[position]
        holder.title?.text = item.title
        holder.description?.text = item.description
        holder.date?.text = item.date
    }
    override fun getItemCount(): Int {
        Log.d("COUNT",".getItemCountcalled ${list.size}")
        return list.size
    }
} 
阅读全文

▼ 版权说明

相关文章也很精彩
推荐内容
更多标签
相关热门
全站排行
随便看看

错说 cuoshuo.com —— 程序员的报错记录

部分内容根据CC版权协议转载;网站内容仅供参考,生产环境使用务必查阅官方文档

辽ICP备19011660号-5

×

扫码关注公众号:职场神器
发送: 1
获取永久解锁本站全部文章的验证码