【问题标题】:ExpandableListView with no children throws indexOutOfBoundsException没有子级的 ExpandableListView 抛出 indexOutOfBoundsException
【发布时间】:2020-10-10 22:44:04
【问题描述】:

我有一个ExpandableListView,其中有些组有孩子,有些没有,我需要做的是只扩展有孩子的组。

body 数组元素的一部分是空的,因此,我得到一个 IndexOutOfBoundsException

    class ExpandableInnerCartAdapter(
        var context: Context,
        var expandableListView: ExpandableListView,
        var header: MutableList<Cart>,
        val isTerminadoFragment:Boolean
    ) : BaseExpandableListAdapter() {
    
        val map = SparseBooleanArray()
        var body: List<List<String>> = listOf()
    
        override fun getGroup(groupPosition: Int): Cart {
           return header[groupPosition]
        }
    
        override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean {
           return true
        }
    
        override fun hasStableIds(): Boolean {
            return false
        }
    
        fun getCart(): MutableList<Cart> = header
        fun getCheckedArray():SparseBooleanArray = map
    
        override fun getGroupView(
            groupPosition: Int,
            isExpanded: Boolean,
            convertView: View?,
            parent: ViewGroup?
        ): View {
    
            var convertView = convertView
            if(convertView == null){
                val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
                convertView = inflater.inflate(R.layout.layout_group,null)
            }
            val item = header[groupPosition]
            body = listOf(item.optionList)
            expandableListView.expandGroup(groupPosition)
            expandableListView.setGroupIndicator(null)
            convertView.item_name.text = item.productName
           
            return convertView
    
        }
    
        override fun getChildrenCount(groupPosition: Int): Int {
            return body[groupPosition].size
        }
    
        override fun getChild(groupPosition: Int, childPosition: Int): Any {
            return body[groupPosition][childPosition]
        }
    
        override fun getGroupId(groupPosition: Int): Long {
            return groupPosition.toLong()
        }
    
        override fun getChildView(
            groupPosition: Int,
            childPosition: Int,
            isLastChild: Boolean,
            convertView: View?,
            parent: ViewGroup?
        ): View {
            var convertView = convertView
            if(convertView == null){
                val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
                convertView = inflater.inflate(R.layout.layout_child,null)
            }
            if(getChildrenCount(groupPosition) > 0){
                val title = convertView?.findViewById<TextView>(R.id.tv_title)
                title?.text = "Opción ${childPosition+1} -> ${getChild(groupPosition,childPosition)}"
            }
    
            return convertView!!
        }
    
        override fun getChildId(groupPosition: Int, childPosition: Int): Long {
            return childPosition.toLong()
        }
    
        override fun getGroupCount(): Int {
            return header.size
        }
    }

当没有小组有孩子并尝试这样做时,似乎会发生错误

    expandableListView.expandGroup(groupPosition)

我已尝试使用 if 语句解决此问题:

    if(body.isNotEmpty()){
        expandableListView.expandGroup(groupPosition)
    }

但是这个解决方案不起作用。

如何避免没有孩子的群体?

谢谢

【问题讨论】:

    标签: android kotlin android-listview expandablelistview


    【解决方案1】:

    当您使用 Kotlin 时,您可以使用许多有用的扩展功能。其中之一是filter,当然您可以为其指定条件。

    解决方案是从您设置为新 body 值的列表中过滤掉空数组:

        body = listOf(item.optionList).filter { it.isNotEmpty() }
    

    过滤函数定义可见here

    【讨论】:

      猜你喜欢
      • 2018-07-31
      • 2023-03-28
      • 1970-01-01
      • 2013-05-20
      • 1970-01-01
      • 2013-06-20
      • 1970-01-01
      • 2020-05-11
      • 2021-12-20
      相关资源
      最近更新 更多