这是一个非常基本的问题。
就是这样,我有一个基本的 Spinner 用如下的监听器实现:
Spinner sensModeSpinner = (Spinner) findViewById(R.id.SensorModeSpinner);
ArrayAdapter<CharSequence> sensModeAdapter = ArrayAdapter.createFromResource(this, R.array.sensorMode_array, android.R.layout.simple_spinner_item);
sensModeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sensModeSpinner.setAdapter(sensModeAdapter);
sensModeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
sMode = parent.getItemAtPosition(pos).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(getBaseContext(), "You need to select atleast one mode!", Toast.LENGTH_SHORT).show();
}
});
Toast.makeText(getBaseContext(), "sensorMode = " + sMode, Toast.LENGTH_SHORT).show();
问题是sMode
变量结果没有出现在 Toast 消息中(值为 NULL),因为它在OnItemSelected
方法中工作正常。
我哪里错了?