Получение indexOutOfBoundsException с помощью ExpandableListView, пытающегося получить группы без дочерних элементов - PullRequest
0 голосов
/ 21 июня 2020

Итак, у меня есть список, который показывает мои группы , а затем тело является дочерним элементом этих групп, теперь, если у группы нет дочернего элемента, он будет генерировать исключение, говоря, что indexOutOfBoundsException index 1 - size 0, теперь я автоматически расширяю все группы по умолчанию, так и должно быть, но мне не нужно расширять группы, у которых нет дочерних элементов, я пробовал со следующим:

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).filter { it.isNotEmpty }
            if(getChildrenCount(groupPosition) > 0) {
            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
        }
    }

Что не должно расширять группу, если getChildrenCount(groupPosition) <= 0, так что это странно, я думаю, что дочерние элементы не обновляются соответствующим образом, ошибка выдается на

  if(getChildrenCount(groupPosition) > 0) { <--- Here
                expandableListView.expandGroup(groupPosition)
                }

Что, я подозреваю, проблема в getChildrenCount(groupPosition) потому что он не вычисляет это выражение.

Поскольку мой объект Cart имеет список дочерних элементов, мне нужно заполнить дочерние элементы внутри getGroupView(), я что-то упускаю?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...