Click here to Skip to main content
13,625,272 members
Rate this:
 
Please Sign up or sign in to vote.
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.

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

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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[^]
  Permalink  
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 :)
Peter Leow 21-Dec-16 7:33am
   
You are welcome.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web02 | 2.8.180712.1 | Last Updated 21 Dec 2016
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100