Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

Java
@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

Java
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
Posted
Updated 20-Dec-16 19:47pm
v2

1 solution

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]+++++
Java
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
C#
sum = + ReceiveAmount;

are you sure your code actually run?
check this out: Operators in Java[^]
 
Share this answer
 
v6
Comments
wseng 21-Dec-16 1:49am    
can you check my post again ? Thanks
Peter Leow 21-Dec-16 1:58am    
If you want to persist the sum value, it has to be declared as a class variable.
See addition in solution 1.
wseng 21-Dec-16 3:52am    
Sir I still getting 5, not 15
Peter Leow 21-Dec-16 6:04am    
There is a syntax error at least, you should have encountered some form of error messages, why didn't you said it out. See solution added.
wseng 21-Dec-16 7:31am    
thanks, I change to += and it works now :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900