我有一个带有微调器的应用程序,其中包含多个项目。 我创造了它和所有
有人能举例说明我如何将这些值传递给我的列表吗?使用mutableList?
干杯
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<*>) {
}
}
答案 0 :(得分:2)
我相信你可以这样做。 1.制作自定义适配器或适配器 2.制作包含字符串值的第一个列表 3.制作包含所选值的可变列表 4.第一次加载微调器时,它从第一个适配器和列表加载值 5.当用户选择项目时,它清除第一个适配器,然后通知更改,然后从可变列表中设置新的适配器加载值(我不确定最后是否需要通知对适配器的更改)
//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
}
}
答案 1 :(得分:0)
创建一个列表来存储selectedItems:
val selectedItems = mutableListOf<String>()
然后在选中项目时添加项目:
override fun onItemSelected(arg0: AdapterView<*>, arg1: View, position: Int, id: Long) {
selectedItems.add(spinnerArray[position])
}