Sum up value from OnActivityResult

221 views Asked by At

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.

 double sum =0;

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 ReceiveAmount = data.getDoubleExtra("amount",0);
             sum = + ReceiveAmount;
            totalAmount.setText(sum+")
             }
         }
     }
 }

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.

Edit

   @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) {
                   double ReceiveAmount = data.getDoubleExtra("amount",0);
                    sum += ReceiveAmount;
                    if (mClickedPosition == -1) {  // if icon clicked
                        if (obj != null) {
                            obj.addNewItem(ReceiveAmount);
                            listview.setAdapter(obj);
             Toast.makeText(getApplicationContext(),sum+"",Toast.LENGTH_SHORT).show();
                            addOrRemoveFooter(sum);
                        }
                    } 
                }
            }
        }

  public void addOrRemoveFooter(double sum2) {
    if (search.size() == 0 && listview.getFooterViewsCount() > 0) {
        listview.removeFooterView(footerLayout);
        Toast.makeText(getApplication(),"In"+sum2+"",Toast.LENGTH_SHORT).show();
    } else if (listview.getFooterViewsCount() == 0 && search.size() > 0) {
        Toast.makeText(getApplication(),"Out"+sum2+"",Toast.LENGTH_SHORT).show();
        listview.addFooterView(footerLayout);
        listview.setAdapter(obj);
        totalAmount.setText(sum2 + "");
    }
    else
    {
        Toast.makeText(getApplicationContext(),"error",Toast.LENGTH_SHORT).show();
    }
}

The toast now display 15, but the setText always 5...Why ?

At first everything seems fine as it display Out in addOrRemoveFooter Toast.makeText(getApplication(),"Out"+sum2+"",Toast.LENGTH_SHORT).show();,but when I add the second time it display error.

4

There are 4 answers

0
John On BEST ANSWER

In order to solve the add up problem, just change =+ to +=

5
Febi M Felix On

Store your amount value in Sharedpreference. Sum up the value from preference each time you return to Activity A.

1
Sushant Gosavi On

Make it variable sum static

public static double sum =0;

than add when ever and where ever you want in app and just display in onActivityResult no need to pass through intent (make sure reinitialize after use)

4
Parin Parikh On

First of all start your activity with startActivityForResult like this way.

private static final int RC_B = 123;
Intent intent = new Intent(ActivityA.this, ActivityB.class);    
activityA.startActivityForResult(intent, RC_B);

then in Activity B override onFinish() and in onFinish

@Override
public void finish() {
  Intent resultIntent = new Intent();
  resultIntent.putExtra("total", 45);  // For Example.
  setResult(RESULT_OK, resultIntent);
  super.finish();
}

then after override onActivityResult in Activity A and put the code like below.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)         {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == Activity.RESULT_OK && requestCode == RC_B) {
         // get your value of sum from intent
         total = intent.getIntExtra("total",0);
    }
}