无法从微调器中检索值

时间:2012-10-23 08:51:16

标签: java android spinner

这是一个非常基本的问题。

就是这样,我有一个基本的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方法中正常工作。

我哪里错了?

3 个答案:

答案 0 :(得分:0)

您在设置sMode值之前执行Toast.show();方法,而是在设置sMode值后将其放在onItemSelected()中,将代码更改为以下内容:

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();
            Toast.makeText(getBaseContext(), "sensorMode = " + sMode, Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            Toast.makeText(getBaseContext(),
                    "You need to select atleast one mode!",
                    Toast.LENGTH_SHORT).show();
        }
    });

答案 1 :(得分:0)

你可以这样做

    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();
        //CALL_YOUR_FUNCTION_HERE
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        Toast.makeText(getBaseContext(), "You need to select atleast one mode!", Toast.LENGTH_SHORT).show();
    }
});

答案 2 :(得分:0)

有些时候可能是愚蠢的错误所以试试这个..

@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

   // sMode = parent.getItemAtPosition(pos).toString();

     sMode = parent.getAdapter().getItemAtPosition(pos).toString();

}