我正在尝试使用 anint
打开不同的图表。我希望它int
在按下按钮时改变值,但它没有改变。这是我的代码:
public class PatientDemographics extends Activity
{
private GraphicalView mChartView;
int num = 1;
int tabnum = 0;
int finalnum;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.tabviews);
LinearLayout ll = (LinearLayout) findViewById(R.id.buttons);
Button button1 = new Button(this);
button1.setText("Disease Type");
ll.addView(button1);
button1.setOnClickListener( new View.OnClickListener()
{
public void onClick(View v)
{
num = 1;
Intent i = new Intent(PatientDemographics.this, TabHelper.class);
i.putExtra("num", tabnum);
startActivity(i);
}
});
Button button2 = new Button(this);
button2.setText("Gender");
ll.addView(button2);
button2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
num = 2;
Intent i = new Intent(PatientDemographics.this, TabHelper.class);
i.putExtra("num", tabnum);
startActivity(i);
}
});
Button button3 = new Button(this);
button3.setText("Age at Diagnosis");
ll.addView(button3);
button3.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
num = 3;
Intent i = new Intent(PatientDemographics.this, TabHelper.class);
i.putExtra("num", tabnum);
startActivity(i);
}
});
Button button4 = new Button(this);
button4.setText("Treatment Status");
ll.addView(button4);
button4.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
num = 4;
Intent i = new Intent(PatientDemographics.this, TabHelper.class);
i.putExtra("num", tabnum);
startActivity(i);
}
});
}
}
请忽略tabnum
。它用于确定所有内容所在的选项卡,它是相同的。此外,我最初使用 xml 创建按钮,但每个选项卡需要不同数量的按钮,我不知道如何做到这一点,所以我只是用代码制作它们。
您可以看到我初始化num
为1
,然后尝试在onClick
方法中更改它,但它永远不会改变。如果您需要更多信息,请告诉我。
编辑:
protected void onSaveInstanceState(Bundle outState)
{
outState.putInt("num", num);
super.onSaveInstanceState(outState);
}
然后在 onCreate 中:
super.onCreate(savedInstanceState);
if(savedInstanceState != null)
{
num = savedInstanceState.getInt("num");
}