例如,我有一个像这样的方法。如果你看到字符串'tablenumber'我希望能够在稍后的函数中使用它,例如onclick按钮,这样我就可以将它的内容发送到另一个活动。但是,如果我在方法之外使用此变量,则无法识别。我该怎么做呢。
{
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String tablenumber = (String) arg0.getSelectedItem();
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
答案 0 :(得分:3)
您可以将整个活动的变量范围设为全局
public class Xyz extends Activity {
String tablenumber;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.abc);
}
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
tablenumber = (String) arg0.getSelectedItem();
}
}
答案 1 :(得分:2)
将其设为活动类中的字段。如果你正在使用eclipse,选择你的局部变量并使用Ctrl / 1或Cmd / 1,然后选择“将局部变量转换为字段”
答案 2 :(得分:2)
您应该使用全局变量。
{
String tablenumber;
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
tablenumber = (String) arg0.getSelectedItem();
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});