0

好的,我有一个用字符串填充的微调器,如何使用数组为整数赋值?

例如微调器有

  1. “一些价值
  2. “另一个值”等

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

我已经包含了一些注释代码来说明我想要达到的目的,在这个例子中,我有一个名为 'actLevel' 的 int 被填充。

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
    }
}
4

1 回答 1

0

这最终奏效了:

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;

            }

        }
于 2013-05-08T19:21:38.343 回答