Home > Software engineering >  Spinner Listener LiveData Issue
Spinner Listener LiveData Issue

Time:10-07

In my Fragment, I have two Spinners.

Both are populated by LiveData which is observed in the ViewModel as below:

      // Observe Filtered ProductGroups and populate Spinner
        businessViewModel.filteredAppDataProductGroups.observe(viewLifecycleOwner, { productGroupArrayList ->
            if (!productGroupArrayList.isNullOrEmpty()){
                val adapter = ArrayAdapter(requireContext(), android.R.layout.simple_spinner_dropdown_item, productGroupArrayList)
                binding.inventoryAddEditProductGroupSpinner.adapter = adapter
            }
        })


// Observe Filtered ProductTypes and populate Spinner
        businessViewModel.filteredAppDataProductTypes.observe(viewLifecycleOwner, { productTypeArrayList ->
            if (productTypeArrayList != null){
                val adapter = ArrayAdapter(requireContext(), android.R.layout.simple_spinner_dropdown_item, productTypeArrayList)
                binding.inventoryAddEditProductTypeSpinner.adapter = adapter
            }
        })

This works fine, but I am trying to filter the data in the second Spinner based on the current selection of the first, by setting a listener and updating Viewmodel data as below:

        binding.inventoryAddEditProductGroupSpinner.onItemSelectedListener = object: AdapterView.OnItemSelectedListener{

            override fun onItemSelected(parent:AdapterView<*>, view: View, position: Int, id: Long){

                val productGroupObject = parent.selectedItem as ProductGroupObject
                if (productGroupObject.productGroupID.isNotEmpty()){
                    businessViewModel.updateCurrentProductGroupVMLiveData(productGroupObject.productGroupID)
                }
            }

            override fun onNothingSelected(parent: AdapterView<*>){
            }
        }

Which updates the filtered list in the viewmodel as below:

    fun updateCurrentProductGroupVMLiveData (currentProductGroupId: String) {
        val newProductGroup = allAppDataProductGroups.value?.find { productGroup -> productGroup.productGroupID == currentProductGroupId }
        _currentProductGroup.value = newProductGroup
        if(newProductGroup?.productGroup != null) {
            val filteredProductsList = allAppDataProductTypes.value?.filter { productTypeObject -> productTypeObject.productGroup == newProductGroup.productGroup} as ArrayList<ProductTypeObject>
            _filteredAppDataProductTypes.value = filteredProductsList
        }
        Log.d(TAG, "updateCurrentProductGroupVMLiveData(): '_currentProductGroupId.value' updated ($currentProductGroupId)")
    }

However, if I add the code shown into onItemSelected, the app crashes and I see the following error:

    --------- beginning of crash
2021-10-06 23:06:34.740 17510-17510/com.xxxxx.acorn E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.locators.acorn, PID: 17510
    java.lang.NullPointerException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkNotNullParameter, parameter view
        at com.xxxxx.acorn.business.inventory.InventoryAddEditFragment$setSpinnerListeners$2.onItemSelected(InventoryAddEditFragment.kt)
        at android.widget.AdapterView.fireOnSelected(AdapterView.java:931)
        at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:920)
        at android.widget.AdapterView.-wrap1(AdapterView.java)
        at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:890)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

I'm seeing that the viewmodel list is changing, so I'm at a loss to why this is causing a NullPointerException..

CodePudding user response:

Seems the error log pointing to view (at 3rd line)

java.lang.NullPointerException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkNotNullParameter, parameter view

I think those parent: AdapterView<*> and view: View are nullable type. Therefore need to add null safe call operator for that type

binding.inventoryAddEditProductGroupSpinner.onItemSelectedListener = object: AdapterView.OnItemSelectedListener{

         override fun onItemSelected(parent:AdapterView<*>?, view: View?, position: Int, id: Long){
            ...
         }

         override fun onNothingSelected(parent: AdapterView<*>?){
         }
     }

CodePudding user response:

You can simplify and clean your code. Just pass the selected position to your viewmodel

 override fun onItemSelected(parent:AdapterView<*>, view: View, position: Int, id: Long){
     businessViewModel.updateCurrentProductGroup(position)           
}

And inside your viewmodel class write the main business logic. like the following

fun updateCurrentProductGroup(position: Int){
    // here you can do what you want
    // example 
    val productGroupObject = filteredAppDataProductGroups.value[position]
    if (productGroupObject.productGroupID.isNotEmpty()){
            businessViewModel.updateCurrentProductGroupVMLiveData(productGroupObject.productGroupID)
    }
}
  • Related