Java 调用clickHandler方法并传递值
我有以下微调器代码:Java 调用clickHandler方法并传递值,java,android,eclipse,parameter-passing,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();
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.
}
目前,Eclipse在spinnerYearsPos参考的下方加上了红色。如何调用clickHandler方法,然后将spinnerYearsPos变量传递给它?///将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.
}
////在处理程序外部创建以下方法
private void open_new_act1()
{
Intent myIntent = new Intent(getApplicationContext(), Table.class);
myIntent.putExtra("spinnerYearsPos", spinnerYearsPos);
startActivity(new Intent(getApplicationContext(), Table.class));
}
你好,曼尼希。非常感谢。我对你的代码所做的唯一改动是我用“myIntent”替换了startActivity方法中的参数,它工作得非常好!
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));
}