我在这里尝试从下拉微调器中显示所选值,但我的setOnItemSelectedListener
无法正常工作
我的代码如下: -
ArrayAdapter<String> adapter =new ArrayAdapter<String>(this, R.array.location,list);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
Toast.makeText(
parent.getContext(),
"The color is "
+ parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
答案 0 :(得分:1)
ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this,R.array.location,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
Toast.makeText(
parent.getContext(),
"The color is "
+ spinner.getSelectedItem().toString(),
Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
进行必要的更改并尝试上述代码。如果不起作用,请将Charsequence
更改为String
。
答案 1 :(得分:0)
试试这段代码
spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Employee employee = employees.get(position);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});