Jarnojr
Jarnojr

Reputation: 714

Material Chips: java.lang.IndexOutOfBoundsException when checked any of the chips

I have a chip group with some chips in my AddItemFragment. I am creating these chips programmatically. To be more precise here is my code to create them:

     mViewModel.categories.observe(viewLifecycleOwner) { resource ->
                handleResource(
                    resource = resource,
                    successFunc = {
                        setCategoryChips(resource.data!!)
                    },
                    errorFunc = {
                        requireView().snack(resource.message!!, R.color.color_danger)
                    }
                )            
        }
    private fun setCategoryChips(categories: List<Kitchen>) {
        for (i in categories.indices) {
            val chip = layoutInflater.inflate(R.layout.my_chip, binding.chipGroup, false) as Chip
            if (i == 0) {
                selectedChip = chip.text.toString()
                chip.isChecked = true
            }
            chip.text = categories[i].name
            binding.chipGroup.addView(chip)
        }
        binding.chipGroup.setOnCheckedChangeListener { group, checkedId ->
            if (group.childCount > 0) {
                selectedChip = (group.get(checkedId -1) as Chip).text.toString()
                Log.i(TAG, "setCategoryChips: $selectedChip")
            }
        }
    }

As you may see I am getting a list from the server then creating chips based on the list. I am getting the text of the checked chip using:

binding.chipGroup.setOnCheckedChangeListener { group, checkedId ->
            if (group.childCount > 0) {
                selectedChip = (group.get(checkedId -1) as Chip).text.toString()
                Log.i(TAG, "setCategoryChips: $selectedChip")
            }
        }

The first time I navigate to my AddItemFragment this method works as expected. But when I navigate to another destination then navigating back to AddItemFragment again, if I check any of the chips I am getting this error

 java.lang.IndexOutOfBoundsException: Index: 29, Size: 28
        at androidx.core.view.ViewGroupKt.get(ViewGroup.kt:32)
        at com.example.semekyepeti.view.fragments.sellerflow.AddItemFragment$setKitchenChips$1.onCheckedChanged(AddItemFragment.kt:183)
        at com.google.android.material.chip.ChipGroup.setCheckedId(ChipGroup.java:362)
        at com.google.android.material.chip.ChipGroup.setCheckedId(ChipGroup.java:355)
        at com.google.android.material.chip.ChipGroup.access$900(ChipGroup.java:56)
        at com.google.android.material.chip.ChipGroup$CheckedStateTracker.onCheckedChanged(ChipGroup.java:546)
        at com.google.android.material.chip.Chip.setChecked(Chip.java:714)
        at android.widget.CompoundButton.toggle(CompoundButton.java:135)
        at android.widget.CompoundButton.performClick(CompoundButton.java:140)

I realize that when I navigate to another destination then navigating back to AddItemFragment again, the checkedId incrementing itself.

 I/AddItemFragment: navigate 0 time
 I/AddItemFragment: checkedId: 2
 I/AddItemFragment: item count: 28

 I/AddItemFragment: navigate 1 time
 I/AddItemFragment: checkedId: 30
 I/AddItemFragment: item count: 28

So I changed checkedId -1 with (checkedId - listCount * navigateCount) -1 to handle this:

      binding.chipGroup.setOnCheckedChangeListener { group, checkedId ->
            if (group.childCount > 0) {
                selectedChip =
                    (group.get((checkedId - 28 * navigateCount) - 1) as Chip).text.toString() //workaround
            }
        }

But I know this is inefficient.I want to get the text of the checked chip. How do I solve this?

Upvotes: 0

Views: 143

Answers (1)

cactustictacs
cactustictacs

Reputation: 19524

The checkedId you're getting is apparently a view ID, and you're meant to grab the actual chip with findViewById. You're using it as an index in the viewgroup, which isn't necessarily going to match (but sometimes does!)

And no the documentation doesn't actually explain this

Upvotes: 1

Related Questions