views:

30

answers:

1

Hello,

I have the following code for a spinner:

public class MyOnItemSelectedListener implements OnItemSelectedListener {

 public void onItemSelected(AdapterView<?> parent,
     View view, int pos, long id) { 

  String TABLE_NAME = parent.getItemAtPosition(pos).toString();

            int spinnerYearsPos = parent.getSelectedItemPosition();

  Cursor cursor = getStats(TABLE_NAME);           

  showStats(cursor);
 }

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

}

What I would like to do is be able to pass the spinnerYearsPos variable in the above code into this method:

public void clickHandler(View v ){

  if (v.getId() == R.id.TableTab) {

   Intent myIntent = new Intent(getApplicationContext(), Table.class);


      myIntent.putExtra("spinnerYearsPos", spinnerYearsPos);
       startActivity(new Intent(getApplicationContext(), Table.class));
      }

      if (v.getId() == R.id.OtherStatsTab) {

      startActivity(new Intent(getApplicationContext(), OtherStats.class));

      }

      }  

At present Eclipse is underlining the spinnerYearsPos reference in red. How do I call the clickHandler method and then pass the spinnerYearsPos variable into it?

A: 

///Take spinnerYearsPos as global var

int spinnerYearsPos; 
   public class MyOnItemSelectedListener implements OnItemSelectedListener {


    public void onItemSelected(AdapterView<?> parent,
         View view, int pos, long id) { 

      String TABLE_NAME = parent.getItemAtPosition(pos).toString();

                spinnerYearsPos = parent.getSelectedItemPosition();

      Cursor cursor = getStats(TABLE_NAME);           

      showStats(cursor);
     }

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

 public void clickHandler(View v ){

      if (v.getId() == R.id.TableTab) {

       open_new_act1();
          }

          if (v.getId() == R.id.OtherStatsTab) {

          startActivity(new Intent(getApplicationContext(), OtherStats.class));

          }

          }  

////create this below method outside handler

private void open_new_act1()
{
Intent myIntent = new Intent(getApplicationContext(), Table.class);


      myIntent.putExtra("spinnerYearsPos", spinnerYearsPos);
       startActivity(new Intent(getApplicationContext(), Table.class));
}
Maneesh
Hi Maneesh. Thank you very much for this. The only bit I changed from your code was I replaced the parameter in the startActivity method with 'myIntent' and it worked perfectly!
Sumino7