gpt4 book ai didi

java - 调用clickHandler方法并传递值

转载 作者:行者123 更新时间:2023-12-01 05:52:57 25 4
gpt4 key购买 nike

我有以下旋转器代码:

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 变量传递给它?

最佳答案

///将 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));
}

关于java - 调用clickHandler方法并传递值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4032806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com