我如何使用onItemSelected方法?

时间:2013-11-26 19:44:48

标签: android spinner

    int I, E
    String[] type;
    Spinner firstSpinner ;
    Spinner secondSpinner ;
    Spinner thirdSpinner;
    Spinner fourthSpinner;    

public void addItemsOnSpinner4(){   
            Spinner firstSpinner = (Spinner) findViewById(R.id.spinner4);
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                    R.array.spinnerarraybool, android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            firstSpinner.setAdapter(adapter);
        }       
        //ACTIONS THAT TAKE PLACE WHEN SPINNER OPTION IS CHOSEN
        public void onItemSelected(AdapterView<?> parent, View view, int arg2,
                long arg3) {
            switch (parent.getId()){
            case R.id.spinner4:
                String firstAnswer = firstSpinner.getSelectedItem().toString();
                if (firstAnswer.equals("True")){
                    E += 1;
                }
                if (firstAnswer.equals("In the Middle")){
                    E += 1;
                    I += 1;
                }
                if (firstAnswer.equals("False")){
                    I += 1;
                }
                break;


<STRINGSXMLstring-array name="spinnerarraybool">
    <item>True</item>
    <item>In the Middle</item>
    <item>False</item>
</STRINGSXMLstring-array>

E和I的值没有变化并保持在0.如何修复onItemSelected方法以使其更改。每当我在我的微调器中选择一个选项时,我希望我和E上升

2 个答案:

答案 0 :(得分:0)

设置适配器

后忘记呼叫firstSpinner.setOnItemSelectedListener(this);

答案 1 :(得分:0)

firstSpinner.setOnItemSelectedListener(new OnItemSelectedListener() 
        {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) 
            {

            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) 
            {

            }
        });