Log says:
D/CartActivity-onCreate(18171): onCreate D/CartActivity-TOTAL-InsideFORLOOP:(18171): 0.0 D/CartActivity-onResume(18171): onResume D/CartAdapter-TOTAL:(18171): 12.95
As you can see above, the log for the loop is executed first in CartActivity and after the onResume() method of the CartActivity method is executed, the CartAdapter executes this line, so I get 0.0 as the Total value in CartActivity inside the For loop
The reason is not where I add the ArrayList data, the problem CartActivity ( where i am getting value for Total ) is executed until CartAdapter ( where i am setting value for Total )
So what do I need to do if I would like to name below the line before executing the onCreate() method of CartActivity
CartArrayList.cartArraylist.get(position).setTotal(totalPrice);
CartActivity.java:
public class CartActivity extends Activity { ..... @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d("CartActivity-onCreate", "onCreate"); setContentView(R.layout.activity_cart); ....... adapter = new CartAdapter(getApplicationContext(), R.layout.adapter_cart, CartArrayList.cartArraylist); for (int d = 0; d < CartArrayList.cartArraylist.size(); d++) { subTotal = subTotal + CartArrayList.cartArraylist.get(d).getTotal(); Log.d("CartActivity-TOTAL-InsideFORLOOP:", String.valueOf(CartArrayList.cartArraylist.get(d).getTotal())); } listview.setAdapter(adapter); adapter.notifyDataSetChanged(); textSubTotal.setText(decimalFormat.format(subTotal)); } @Override public void onResume() { super.onResume(); Log.d("CartActivity-onResume", "onResume"); } }
CartAdapter.java:
public class CartAdapter extends BaseAdapter { ..... @Override public View getView(final int position, View convertView, ViewGroup parent) { ...... totalPrice = cart.getQuantity() * cart.getPrice(); CartArrayList.cartArraylist.get(position).setTotal(totalPrice); Log.d("CartAdapter-TOTAL:", String.valueOf(CartArrayList.cartArraylist.get(position).getTotal())); ..... return convertView; } }
source share