Vivien Scholz
Lurker
I have a fragment where I display Chips at the top containing the names of categories and a recyclerview below containing all the ingredients. The Chips are dynamically created in the Category Observer. When a chip gets checked the recyclerview gets filtered to display the ingredients belonging to the choosen category.
The problem I have, is that when navigating up (pressing back) to go back to the previous fragment and then opening the fragment again, the OnCheckedChangeListener of the chipgroup returns the wrong chip id (in "p1"). Therefore I get a outOfBoundsException since there are only 9 children.
Example:
first run -> id: 1 (first item) -> correct
second run -> id: 19 (first item) -> false
third run -> id: 28 (first item) -> false
..and so on.
The problem I have, is that when navigating up (pressing back) to go back to the previous fragment and then opening the fragment again, the OnCheckedChangeListener of the chipgroup returns the wrong chip id (in "p1"). Therefore I get a outOfBoundsException since there are only 9 children.
Example:
first run -> id: 1 (first item) -> correct
second run -> id: 19 (first item) -> false
third run -> id: 28 (first item) -> false
..and so on.
Java:
class BlankFragment2 : Fragment() {
private var ingredientViewModel : IngredientViewModel? = null
private var categoryViewModel : CategoryViewModel? = null
private var adapter : IngredientAdapter? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
ingredientViewModel = ViewModelProvider(activity!!).get(IngredientViewModel::class.java)
categoryViewModel = ViewModelProvider(activity!!).get(CategoryViewModel::class.java)
adapter = IngredientAdapter(context!!)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_blank2, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
(activity as AppCompatActivity).setSupportActionBar(bottombar)
(activity as AppCompatActivity).supportActionBar!!.setDisplayHomeAsUpEnabled(true)
setObserver(adapter!!)
rvIngs.adapter = adapter
rvIngs.layoutManager = GridLayoutManager(context, 2)
chipgroup.setOnCheckedChangeListener { p0, p1 ->
if(p1 != -1){
adapter!!.filter.filter((p0[p1-1] as Chip).text)
} else {
adapter!!.filter.filter(null)
}
}
}
private fun setObserver(adapter: IngredientAdapter){
ingredientViewModel!!.getIngs().observe(viewLifecycleOwner, Observer {
adapter!!.setIngCopyList(it)
adapter!!.submitList(it)
})
categoryViewModel!!.getCats().observe(viewLifecycleOwner, Observer {
adapter.setCatCopyList(it)
for (cat in it){
val chip = layoutInflater.inflate(R.layout.chip_style, chipgroup, false) as Chip
chip.text = cat.catName
chip.isCheckable
chip.isClickable
chipgroup.addView(chip)
}
})
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.ing_bot_menu, menu)
super.onCreateOptionsMenu(menu, inflater)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
android.R.id.home -> findNavController().navigateUp()
}
return super.onOptionsItemSelected(item)
}
}