0

問題のようです...私のプログラムは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;

        }
}
4

1 に答える 1

0

ユニタ値をグローバルに宣言して、他の方法で使用します。

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) {}
});
于 2013-01-19T15:36:59.060 に答える