使用复选框实现 Android Recycler View 时出现“空列表不包含索引 0 处的元素”错误

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

我正在实现一个回收器视图,其项目作为复选框。我的数据来自 ROOM 数据库,此回收器视图位于对话框片段内。

对话框片段:

 override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding = ScheduleFloorDialogBinding.inflate(layoutInflater)
        createProfileViewModel = CreateProfileViewModel(Application())
        floorProfileDialogAdapter = FloorProfileDialogAdapter()

        binding.rvFloorsForScheduling.layoutManager = LinearLayoutManager(requireActivity())
        binding.rvFloorsForScheduling.adapter = floorProfileDialogAdapter

        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val floorList: MutableList<String> = mutableListOf()

        //Getting list of all floors
        createProfileViewModel.totalFloors.observe(viewLifecycleOwner) {
            Timber.d("List of floors received : $it")

            val intList = it.map(String::toInt)
            val maxFloorValue = intList.last()
            var count = 0

            try {
                while (count <= maxFloorValue) {
                    floorList.add(count.toString())
                    count++
                }
            } catch (e: Exception) {
                Timber.d("Exception: $e")
            }
            floorProfileDialogAdapter.getAllFloors(floorList)
            Timber.d("Floor List : $floorList")
        }
    }

我可以将数据列表从这里发送到我的适配器。

适配器:

class FloorProfileDialogAdapter() : RecyclerView.Adapter<FloorProfileDialogAdapter.MyViewHolder>() {

    var floors = emptyList<String>()

    inner class MyViewHolder(val binding: ScheduleFloorDialogItemBinding) :
        RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        val binding = ScheduleFloorDialogItemBinding.inflate(inflater, parent, false)

        return MyViewHolder(binding)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val currentFloor = floors[position]
        Timber.d("Current floor: $currentFloor")
        holder.binding.floorCheckBox.text = "Floor $currentFloor"
    }

    override fun getItemCount(): Int {
        return floors.toString().length
    }

    fun getAllFloors(floorsReceived: List<String>) {
        Timber.d("Floors received : $floorsReceived")
        this.floors = floorsReceived
    }
}

登录适配器的

getAllFloor
方法显示已收到列表:

但是在

onBindViewHolder()
内部,当我使用
position
时,我收到错误消息:

java.lang.IndexOutOfBoundsException: Empty list doesn't contain element at index 0.
android kotlin checkbox android-recyclerview
2个回答
1
投票

当您使用空列表初始化 FloorProfileDialogAdapter 时,视图已经膨胀,直到您使用 notificationDataSetChange() 为止它不会更改 这是不推荐的解决方案

或者 使用 Androidx Recycler 视图包中的 ListAdapter:它有自己的提交列表,因此每次您提交列表时,它都会通知数据集更改,并将其与前一个进行比较

检查文档


0
投票

就我而言,我用

PagingDataAdapter
替换了常用的自定义适配器。错误 “java.lang.IndexOutOfBoundsException:空列表不包含索引 0 处的元素。”发生在
onBindViewHolder
:

var items: List<Item> = emptyList()

override fun onBindViewHolder(holder: SomeViewHolder<*>, position: Int) {
    val item = items[position]

创建适配器后,

items
集合尚未填充。然后我替换为:

override fun onBindViewHolder(holder: SomeViewHolder<*>, position: Int) {
    val item = getItem(position)
© www.soinside.com 2019 - 2024. All rights reserved.