Android Koltin pass spinner values to mutable list

2019-07-27 04:48发布

问题:

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])

   }