我的Spinner OnItemSelected函数从未调用过。 当我单击微调器时,将显示列表。但是,当我单击列表中的某个项目时,什么也没发生。
val adapter = ArrayAdapter(activity!!,android.R.layout.simple_spinner_item,nameOfClients)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
mClientsSpinner.setAdapter(adapter)
mClientsSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
Toast.makeText(activity!!,nameOfClients[position],Toast.LENGTH_SHORT).show()
selectedClient = nameOfClients[position]
}
override fun onNothingSelected(parent: AdapterView<*>?) {
}
}
答案 0 :(得分:0)
spinner.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
Object item = parent.getItemAtPosition(pos);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
答案 1 :(得分:0)
以下代码对我有用,列表为ArrayList<String>
val subjectAdapter = ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list)
//set drop down resource
subjectAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
//set Adapter
spSelectSubject.adapter = subjectAdapter
//set listner
spSelectSubject.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
//your own method
}
override fun onNothingSelected(parent: AdapterView<*>) {}
}
答案 2 :(得分:0)
您可以显示活动的所有代码吗? 是活动,片段还是适配器?
我认为,问题出在“上下文”上