微调器开关盒问题

时间:2010-12-18 03:04:53

标签: android

编辑:我已添加了我的所有代码(不包括包和导入.....)如果我尝试运行它崩溃......任何想法为什么?

public class BaseConverter extends Activity {
/** Called when the activity is first created. */
int inputBase;
int outputBase;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Spinner input_spinner = (Spinner) findViewById(R.id.InputSpinner);
    Spinner output_spinner = (Spinner) findViewById(R.id.OutputSpinner);

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.base_numbers_array, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    input_spinner.setAdapter(adapter);
    output_spinner.setAdapter(adapter);

    input_spinner.setOnItemSelectedListener(new InputItemSelectedListener());

    output_spinner.setOnItemSelectedListener(new OutputItemSelectedListener());
}

public class InputItemSelectedListener implements OnItemSelectedListener {

    public void onItemSelected(AdapterView<?> parent,
        View view, int pos, long id)
        {
        /*  switch (Integer.parseInt(parent.getItemAtPosition(pos).toString())
            case ((Integer)parent.getItemAtPosition(pos)).intValue(); 
                inputBase = 2;
                break;
            case 8:
                inputBase = 8;
                break;
            case 10;
                inputBase = 10;
                break;
            case 16;
                inputBase = 16;
                break;
                */

        Toast.makeText(parent.getContext(), "You selected input base " +
                parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();
        }

    public void onNothingSelected(AdapterView parent) 
    {
      // Do nothing.
    }
}

public class OutputItemSelectedListener implements OnItemSelectedListener {

    public void onItemSelected(AdapterView<?> parent,
        View view, int pos, long id) { 
      Toast.makeText(parent.getContext(), "You selected output base " +
          parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();
    }

    public void onNothingSelected(AdapterView parent) {
      // Do nothing.
    }
} 

}

现在需要一个切换案例场景,所有场景都围绕着他们选择它的价值。它们都是数字(选项)并存储在整数数组中。如何正确设置开关盒?我试过做一个简单的事情,比如

 case ((Integer.parseInt(parent.getItemAtPosition(pos).toString())

1 个答案:

答案 0 :(得分:1)

所以我想通了。你需要使数组成为一个STRING数组并使用:     ArrayAdapter adapter = new ArrayAdapter

等.....

然后使用

Integer.parseInt(parent.getItemAtPosition(position).toString());

找到您选择的任何数值。注意:它必须是全数字或它会给你一个错误。