调用clickHandler方法并传递值

时间:2010-10-27 11:41:23

标签: java android eclipse parameter-passing

我有一个微调器的代码:

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.
 }

}

我想要做的是能够将上面代码中的spinnerYearsPos变量传递给这个方法:

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));

      }

      }  

目前Eclipse正在用红色标注spinnerYearsPos引用。如何调用clickHandler方法然后将spinnerYearsPos变量传递给它?

1 个答案:

答案 0 :(得分:0)

///将spinnerYearsPos作为全局变量

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));

          }

          }  

////在处理程序外部创建此方法

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


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