-1

I want to pass a from my method to the button clicked method.

Method that generates a:

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
             String str = arg0.getItemAtPosition(arg2).toString();

             double a = 0;

             switch(arg2){

             case 0:
                  a = 4;
                 break;

             case 1:
                 a = 3.75;
                 break;

             case 2:
                 a = 3.50;
                 break;

             case 3:
                 a = 3.25;
                 break;
             case 4:
                 a = 3;
                 break;
             case 5:
                 a = 2.75;
                 break;
             case 6:
                 a = 2.5;
                 break;
             case 7:
                 a = 2.25;
                 break;


             }

Button clicked method

public void button(View v){

}
MrEngineer13
  • 38,642
  • 13
  • 74
  • 93
  • 2
    there is no question here – tyczj Sep 16 '14 at 17:39
  • where is your "button method" ? – Dave Sep 16 '14 at 17:40
  • 2
    You are falling victim to scope. You need to make a's scope the entire class instead of just your method. See MrEngineer13's answer for how to move it to a class field variable. Long story short, move the declaration for a to just inside of your activity class scope. – zgc7009 Sep 16 '14 at 17:49
  • http://stackoverflow.com/questions/10614696/how-to-pass-parameters-to-onclicklistener/10614751#10614751 Shows you how to create your own click listener in to which you can pass additional paramaters – Chris Nevill Sep 16 '14 at 17:52

1 Answers1

4

Make a a field of your activity instead of an instance variable like so:

public class MyActivity extends Activity {

double a = 0;

...

}
MrEngineer13
  • 38,642
  • 13
  • 74
  • 93
  • 2
    What code? all you need to change in your code is to cut `double a = 0;` line and paste it at the top of your activity class – MrEngineer13 Sep 16 '14 at 17:51