Spinner不能在Android中的条件语句中使用

时间:2013-01-19 15:15:24

标签: android spinner android-arrayadapter

什么似乎是问题......我的程序无法进入switch语句。我将位置分配给unita,当我在switch语句中使用它时,它没有响应。但是当我尝试将代码放在switch语句之外时,程序会正常响应。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

   ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, list);
   category = (Spinner) findViewById(R.id.categoryList);
   category.setAdapter(adapter);
   category.setOnItemSelectedListener(this);

}

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) 
{
    // TODO Auto-generated method stub
    int position = category.getSelectedItemPosition();

    switch(position)
    {
        case 0:
        {
            //Length                                
            Length();
        }   
            break;
        case 1:
        {
            //Weight
            Weight();
        }   
            break;
        case 2:
            //Time
            Time();
            break;
        case 3:
            //IT
            IT();
            break;
        case 4:
            //Area
            Area();
            break;
        case 5:
            //Volume
            Volume();
            break;
        case 6:
            //Perimeter
            Perimeter();
            break;
    }               

}
public void Length()
{
    ArrayAdapter<String> lengthadapt = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, length);
    ArrayAdapter<String> lengthadapt2 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, length);

    unit1 = (Spinner) findViewById(R.id.unit1);
    int unita = unit1.getSelectedItemPosition();   
    unit1.setAdapter(lengthadapt);
    unit1.setSelection(unita);
    unit2 = (Spinner) findViewById(R.id.unit2);
    int unitb = unit2.getSelectedItemPosition();
    unit2.setAdapter(lengthadapt2);
    unit2.setSelection(unitb); 
    b1=(ImageButton) findViewById(R.id.convert1);       

    int con=unita;

switch(con)
{
case 0:         
{   b1=(ImageButton) findViewById(R.id.convert1);   
    b1.setOnClickListener(new OnClickListener() {  
      public void onClick(View v) {           
          t1=(EditText) findViewById(R.id.input1);
          t2=(EditText) findViewById(R.id.input2);  
          Double res;
            String txt;
            int num1=Integer.parseInt(t1.getText().toString());
            res=(double)num1*1000;
            System.out.println(res);
            t2.setText(res.toString());
      }});  
    break;
}
case 1:
    {b1=(ImageButton) findViewById(R.id.convert1);  
        b1.setOnClickListener(new OnClickListener() {  
              public void onClick(View v) {           
                  t1=(EditText) findViewById(R.id.input1);
                  t2=(EditText) findViewById(R.id.input2);  
                  Double res;
                    String txt;
                    int num1=Integer.parseInt(t1.getText().toString());
                    res=(double)num1*1000;
                    System.out.println(res);
                    txt=res.toString();
                    t2.setText(txt);

                  }});  
        break;
    }
case 2:{
    b1=(ImageButton) findViewById(R.id.convert1);   
    b1.setOnClickListener(new OnClickListener() {  
          public void onClick(View v) {           
              t1=(EditText) findViewById(R.id.input1);
              t2=(EditText) findViewById(R.id.input2);  
              Double res;
                String txt;
                int num1=Integer.parseInt(t1.getText().toString());
                res=(double)num1*.01;
                System.out.println(res);
                txt=res.toString();
                t2.setText(txt);

              }});  
    break;
}
case 3:
{
    b1=(ImageButton) findViewById(R.id.convert1);   
    b1.setOnClickListener(new OnClickListener() {  
          public void onClick(View v) {           
              t1=(EditText) findViewById(R.id.input1);
              t2=(EditText) findViewById(R.id.input2);  
              Double res;
                String txt;
                int num1=Integer.parseInt(t1.getText().toString());
                res=(double)num1*.001;
                System.out.println(res);
                txt=res.toString();
                t2.setText(txt);

              }});  
    break;
}
case 4:{
    b1=(ImageButton) findViewById(R.id.convert1);   
    b1.setOnClickListener(new OnClickListener() {  
          public void onClick(View v) {           
              t1=(EditText) findViewById(R.id.input1);
              t2=(EditText) findViewById(R.id.input2);  
              Double res;
                String txt;
                int num1=Integer.parseInt(t1.getText().toString());
                res=(double)num1*.4;
                System.out.println(res);
                txt=res.toString();
                t2.setText(txt);

              }});  
    break;
}
case 5:
{
    b1=(ImageButton) findViewById(R.id.convert1);   
    b1.setOnClickListener(new OnClickListener() {  
          public void onClick(View v) {           
              t1=(EditText) findViewById(R.id.input1);
              t2=(EditText) findViewById(R.id.input2);  
              Double res;
                String txt;
                int num1=Integer.parseInt(t1.getText().toString());
                res=(double)num1*12;
                System.out.println(res);
                txt=res.toString();
                t2.setText(txt);

              }});  
    break;
}
}


switch(unitb)
        {
        case 0:

            break;
        case 1:

            break;
        case 2:

            break;
        case 3:

            break;
        case 4:

            break;
        case 5:

            break;

        }
}

1 个答案:

答案 0 :(得分:0)

全局声明您的unita值以在其他方法中使用它。

int unita;

unit1.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
{
unita = arg0.getSelectedItemPosition();
Toast.makeText(getBaseContext(),
"You have selected item position : " + unita,
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {}
});