我如何从onactivityresult总结价值 [英] How do I sum up value from onactivityresult
问题描述
我在活动A中有一个列表视图。我想总结从活动B返回到活动A的总金额,但我不知道如何添加它们。
@覆盖
public void onActivityResult( int requestCode, int resultCode ,Intent数据){ // 从活动B接收并填充ListView A
if (resultCode == RESULT_OK){
if (requestCode == PROJECT_REQUEST_CODE){
ReceiveAmount = data.getStringExtra( amount);
Toast.makeText(getApplication(), 总金额为 + ReceiveAmount,LENGTH_SHORT) 。节目();
}
}
}
}
假设活动BI返回金额5 ,然后我再次去活动B并返回值10.我希望吐司显示值15而不是总是5.
< b>已编辑
public void onActivityResult( int requestCode, int resultCode,Intent data) { // 从活动B接收并填充ListView A
if (resultCode == RESULT_OK){
if (requestCode == PROJECT_REQUEST_CODE){
double sum = 0 ;
ReceiveAmount = data.getStringExtra( amount);
sum = + ReceiveAmount;
Toast.makeText(getApplication(), 总金额为 + sum,LENGTH_SHORT) 。节目();
}
}
}
}
我尝试了什么:
谷歌但未找到答案。谢谢
在活动A中创建一个变量说sum,初始值为零,每当从活动B返回时,将返回的金额添加到此总和 并将其展示在吐司中。这就是全部。
+++++ [第2轮] +++++
< span class =code-keyword> public class MainActivity extends 活动{
double sum = 0 ;
protected void onCreate(Bundle savedInstanceState){
// ...
了解详情了解类成员(Java™教程>学习Java语言>类和对象) [ ^ ]
+++++ [第3轮] +++++
我注意到了这个
sum = + ReceiveAmount;
I have a listview in Activity A. I want to sum up the total amount which are return from Activity B to Activity A, but I have no idea on how to add them up.
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { // receive from Activity B and populate ListView A
if (resultCode == RESULT_OK) {
if (requestCode == PROJECT_REQUEST_CODE) {
ReceiveAmount = data.getStringExtra("amount");
Toast.makeText(getApplication(),"Total Amount is"+ReceiveAmount,LENGTH_SHORT).show();
}
}
}
}
Assume in Activity B I return amount 5, then I go to Activity B again and return value 10. I want the toast display value 15 instead of always 5.
Edited
public void onActivityResult(int requestCode, int resultCode, Intent data) { // receive from Activity B and populate ListView A
if (resultCode == RESULT_OK) {
if (requestCode == PROJECT_REQUEST_CODE) {
double sum = 0;
ReceiveAmount = data.getStringExtra("amount");
sum = + ReceiveAmount;
Toast.makeText(getApplication(),"Total Amount is"+sum,LENGTH_SHORT).show();
}
}
}
}
What I have tried:
Google but no answer found. Thanks
Create a variable say "sum" in activity A with an initial value of zero, whenever it returns from activity B, add the returned amount to this "sum" and display it in toast. That's all.
+++++[round 2]+++++
public class MainActivity extends Activity { double sum = 0; protected void onCreate(Bundle savedInstanceState){ //...
Learn more Understanding Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)[^]
+++++[round 3]+++++
I noticed this
sum = + ReceiveAmount;
are you sure your code actually run?
check this out: Operators in Java[^]
这篇关于我如何从onactivityresult总结价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!