如何将整数存储在 activity 中进行计算
How to store an integer in an activity for calculation
我目前正在创建一个应用程序,它将值加在一起给出总计,就像这样。旧值 + 新值 = textView1...
MainActivity,这里是textView1要存放整体值的地方
String calorie = getIntent().getStringExtra("calorie");
TextView textView1 = (TextView)findViewById(R.id.textView1);
String strOldValue = textView1.getText().toString();
Integer oldValue;
try{
oldValue= Integer.parseInt(myString);
}catch(NumberFormatException e){
oldValue =0;
}
Integer newValue;
try{
newValue= Integer.parseInt(calorie);
}catch(NumberFormatException e){
newValue =0;
}
textView1.setText((oldValue + newValue));
问题是 textview 忘记了旧值而只是插入新值...所以携带 100,然后我去添加 150...我只看到 150。相信我需要将 100 (oldValue) 存储在某处。有什么建议吗?
上下文:这是一个食物计算器,使用 AddActivity 和 MainActivity 添加食物卡路里。
在MainActivity中取一个静态变量
static int count=0;
并获得价值
String calorie = getIntent().getStringExtra("calorie");
并将其添加到
count+=Integer.parseInt(calorie);
并将值设置为edittext
textView1.setText(count+"");
希望对您有所帮助。
当你在 oldValue= Integer.parseInt(myString);
中分配 oldValue 时,它使用 myString,当我们需要使用 strOldValue 时,你可以在这里 String strOldValue = textView1.getText().toString();
问题只是你不注意,祝你好运)
我目前正在创建一个应用程序,它将值加在一起给出总计,就像这样。旧值 + 新值 = textView1...
MainActivity,这里是textView1要存放整体值的地方
String calorie = getIntent().getStringExtra("calorie");
TextView textView1 = (TextView)findViewById(R.id.textView1);
String strOldValue = textView1.getText().toString();
Integer oldValue;
try{
oldValue= Integer.parseInt(myString);
}catch(NumberFormatException e){
oldValue =0;
}
Integer newValue;
try{
newValue= Integer.parseInt(calorie);
}catch(NumberFormatException e){
newValue =0;
}
textView1.setText((oldValue + newValue));
问题是 textview 忘记了旧值而只是插入新值...所以携带 100,然后我去添加 150...我只看到 150。相信我需要将 100 (oldValue) 存储在某处。有什么建议吗?
上下文:这是一个食物计算器,使用 AddActivity 和 MainActivity 添加食物卡路里。
在MainActivity中取一个静态变量
static int count=0;
并获得价值
String calorie = getIntent().getStringExtra("calorie");
并将其添加到
count+=Integer.parseInt(calorie);
并将值设置为edittext
textView1.setText(count+"");
希望对您有所帮助。
当你在 oldValue= Integer.parseInt(myString);
中分配 oldValue 时,它使用 myString,当我们需要使用 strOldValue 时,你可以在这里 String strOldValue = textView1.getText().toString();
问题只是你不注意,祝你好运)