简体   繁体   中英

Android how to assign a spinner position an integer value

Ok, I have a spinner that is populated with strings, how can I use the array to assign a value to an integer?

for instance spinner has

  1. "some value
  2. "another value" etc.

When 1 is selected how can I then initialize a variable based on selection, use an if statement or switch/case?

I have included some commented code to illustrate what I'm trying to get at, in this example I have an int called 'actLevel' to be populated.

public class spinActMultFunction implements OnItemSelectedListener {
    @Override
    public void onItemSelected(AdapterView<?> parent, View arg1, int pos, long id) {
        String str=parent.getItemAtPosition(pos).toString();
        activityMultiplier.setText(str);

        /*
         If (pos) = 1
            then actLevel = 1.2 
        else if (pos) = 2
            then actLevel = 1.6

        etc..
        */
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub
    }
}

this worked in the end:

public void onItemSelected(AdapterView<?> parent, View arg1, int pos,
                long id) {
            String str = parent.getItemAtPosition(pos).toString();
            activityMultiplier.setText(str);

            switch(pos){

            case 0:
                actLevel = 1.2;
                break;

            case 1:
                actLevel = 1.3;
                break;

            case 2:
                actLevel = 1.5;
                break;

            case 3:
                actLevel = 1.7;
                break;
            case 4:
                actLevel = 1.9;
                break;

            }

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM