1. Are you ready for the Galaxy S20? Here is everything we know so far!

Chipgroup OnCheckedChangeListener wrong chip-id

Discussion in 'Android Development' started by Vivien Scholz, Mar 18, 2020.

  1. Vivien Scholz

    Vivien Scholz Lurker
    Thread Starter

    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.

    Code (Java):
    1. class BlankFragment2 : Fragment() {
    2.  
    3.  
    4.     private var ingredientViewModel : IngredientViewModel? = null
    5.     private var categoryViewModel : CategoryViewModel? = null
    6.     private var adapter : IngredientAdapter? = null
    7.  
    8.  
    9.     override fun onCreate(savedInstanceState: Bundle?) {
    10.         super.onCreate(savedInstanceState)
    11.  
    12.         setHasOptionsMenu(true)
    13.  
    14.         ingredientViewModel = ViewModelProvider(activity!!).get(IngredientViewModel::class.java)
    15.  
    16.         categoryViewModel = ViewModelProvider(activity!!).get(CategoryViewModel::class.java)
    17.  
    18.         adapter = IngredientAdapter(context!!)
    19.  
    20.  
    21.  
    22.     }
    23.     override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    24.         return inflater.inflate(R.layout.fragment_blank2, container, false)
    25.     }
    26.  
    27.     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    28.         super.onViewCreated(view, savedInstanceState)
    29.  
    30.         (activity as AppCompatActivity).setSupportActionBar(bottombar)
    31.         (activity as AppCompatActivity).supportActionBar!!.setDisplayHomeAsUpEnabled(true)
    32.  
    33.  
    34.         setObserver(adapter!!)
    35.  
    36.         rvIngs.adapter = adapter
    37.         rvIngs.layoutManager = GridLayoutManager(context, 2)
    38.  
    39.  
    40.  
    41.         chipgroup.setOnCheckedChangeListener { p0, p1 ->
    42.  
    43.             if(p1 != -1){
    44.                 adapter!!.filter.filter((p0[p1-1] as Chip).text)
    45.             } else {
    46.                 adapter!!.filter.filter(null)
    47.             }
    48.         }
    49.  
    50.     }
    51.  
    52.     private fun setObserver(adapter: IngredientAdapter){
    53.  
    54.         ingredientViewModel!!.getIngs().observe(viewLifecycleOwner, Observer {
    55.             adapter!!.setIngCopyList(it)
    56.             adapter!!.submitList(it)
    57.         })
    58.  
    59.  
    60.         categoryViewModel!!.getCats().observe(viewLifecycleOwner, Observer {
    61.             adapter.setCatCopyList(it)
    62.  
    63.             for (cat in it){
    64.                 val chip = layoutInflater.inflate(R.layout.chip_style, chipgroup, false) as Chip
    65.                 chip.text = cat.catName
    66.                 chip.isCheckable
    67.                 chip.isClickable
    68.                 chipgroup.addView(chip)
    69.             }
    70.         })
    71.  
    72.     }
    73.  
    74.  
    75.     override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
    76.         inflater.inflate(R.menu.ing_bot_menu, menu)
    77.         super.onCreateOptionsMenu(menu, inflater)
    78.     }
    79.  
    80.     override fun onOptionsItemSelected(item: MenuItem): Boolean {
    81.         when (item.itemId) {
    82.             android.R.id.home -> findNavController().navigateUp()
    83.         }
    84.         return super.onOptionsItemSelected(item)
    85.     }
    86.  
    87. }
     


  2. Vivien Scholz

    Vivien Scholz Lurker
    Thread Starter

    Found the solution. The id is not realiable in my use case. But you can assign a tag or id with "chip.id", "chip.tag" and use that.
     
Loading...

Share This Page

Loading...