0

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

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

我哪里错了?

4

3 回答 3

0

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

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

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

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

}
于 2012-10-23T09:35:34.460 回答
0

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

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();
        }
    });
于 2012-10-23T08:56:22.757 回答
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();
    }
});
于 2012-10-23T09:06:23.500 回答