1

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<*>) {

        }
    }
Jaydeep
  • 1,686
  • 1
  • 16
  • 29
KirstyHA
  • 29
  • 1
  • 3
  • 1
    Post your code; tell us what you have tried and what's not working. – fweigl Sep 27 '17 at 14:37
  • I am not sure if that is the way you create a mutableList val. And look at the two commented ones I have, are they correct? – KirstyHA Sep 27 '17 at 14:38

2 Answers2

2

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


            }

        }
Rajesh Dalsaniya
  • 1,977
  • 1
  • 13
  • 12
0

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

   }
fweigl
  • 21,278
  • 20
  • 114
  • 205