Android Kotlin pass spinner values to mutable list

二次信任 提交于 2019-12-10 11:37:59

问题


I have an application with a spinner that contains several items. I created it and all that

Could anyone give me an example of how I can pass these values to a list I have? Using a mutableList?

Cheers

class NewKitListActivity : AppCompatActivity() {

var spinnerArray = arrayOf("Dumbell", "Punching Bag", "Yoga Ball", "Skipping Rope")

 val kitMutableList = mutableListOf(spinnerArray)





override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_new_kit_list)



    val spinner = newKitItemSpinner
    val spinnerArrayAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray)
    //selected item will look like a spinner set from XML
    spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
    spinner.adapter = spinnerArrayAdapter

    spinner.onItemSelectedListener = object : OnItemSelectedListener {
        override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
            val selectedItem = parent.getItemAtPosition(position).toString()
            if (selectedItem == "Dumbell") {

               // mutableListAdapter.toMutableList()
                //mutableList.adapter = mutableListAdapter


            }
        } // to close the onItemSelected

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

        }
    }

回答1:


I believe you can do like this. 1. Make custom adapter or adapters 2. Make first list which hold string values 3. Make mutable list which holds selected values 4. when spinner loaded first time it load values from first adapter and list 5. when user select item it clear first adapter then notify changes then set new adapter load values from mutable list ( I am not sure last will require to notify changes to adapter)

//Mutable List for storing selected items

val selectedItems: MutableList<String>? = null 

 //Listen On select for spinner

        spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {

            //Performing action onItemSelected and onNothing selected
            override fun onItemSelected(arg0: AdapterView<*>, arg1: View, position: Int, id: Long) {

               // Add selected item in Mutable List
               selectedItems.add(spinnerArray[position])

               // Clear Adapter
                spinner.adapter = null
                // Notify data set changed
                spinnerArrayAdapter.notifyDataSetChanged()
               // Set New Data adapter
               spinner.adapter = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, selectedItems)


            }

            override fun onNothingSelected(arg0: AdapterView<*>) {
                // TODO: Auto-generated method stub


            }

        }



回答2:


Create a list to store your selectedItems:

val selectedItems = mutableListOf<String>()

then add items when they are selected:

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

           selectedItems.add(spinnerArray[position])

   }


来源:https://stackoverflow.com/questions/46450622/android-kotlin-pass-spinner-values-to-mutable-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!