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 in above Log for loop is executed first in CartActivity and after the execution of onResume() method of CartActivity, CartAdapter is executing this line, therefore for i am getting 0.0 as value of Total in CartActivity inside For loop
The reason is not where i am adding to data ArrayList, issue is CartActivity executes (where i am getting value for Total) before CartAdapter execution (where i am setting value for Total)
So what I have to do, If I would like to call below line before execution of 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;
}
}
You have to calculate Total value in entity only before setting the array list in Adapter, rather than calculating it in getView.
Code snippet :
for (int d=0; d<CartArrayList.cartArraylist.size(); d++) {
// calculate total value
Double totalPrice = CartArrayList.cartArraylist.get(d).getQuantity() * CartArrayList.cartArraylist.get(d).getPrice();
CartArrayList.cartArraylist.get(d).setTotal(totalPrice);
// set it for subTotal
subTotal = subTotal + totalPrice;
}
adapter = new CartAdapter(getApplicationContext(), R.layout.adapter_cart, CartArrayList.cartArraylist);
listview.setAdapter(adapter);
Hope it helps ツ
You're going to want to move your list population to onResume() and it's a good idea to store your list in the outstate bundle of onSaveInstanceState() then check for it's existence to repopulate your list in onCreate()'s savedInstanceState bundle. That's indirectly related to your question ;) I know, but if you're moving the population and display of your list to onResume() you're going to want to also do those things so you're not repeating work you've potentially already done.
Also, move your notifyDataSetChanged() to after the spot where you update the data.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
listview = (ListView)findViewById(R.id.listCart);
if (adapter == null) {
adapter = new CartAdapter(getApplicationContext(), R.layout.adapter_cart, CartArrayList.cartArraylist);
}
listview.setAdapter(adapter);
if (savedInstanceState != null) {
CartArrayList.cartArrayList = savedInstanceState.getParcelableArrayList(KEY_BUNDLE_CART_LIST);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putParcelableArrayList(KEY_BUNDLE_CART_LIST, CartArrayList.cartArrayList);
super.onSaveInstanceState(outState);
}
@Override
public void onResume() {
super.onResume();
if (CartArrayList.cartArrayList.isEmpty()) {
// Update your cartArrayList here if necessary
}
adapter.notifyDataSetChanged();
for(int d=0; d<CartArrayList.cartArraylist.size(); d++) {
subTotal = subTotal + CartArrayList.cartArraylist.get(d).getTotal();
Log.d("NAME:", CartArrayList.cartArraylist.get(d).getName().toString());
Log.d("QUANTITY:", String.valueOf(CartArrayList.cartArraylist.get(d).getQuantity()));
Log.d("PRICE:", String.valueOf(CartArrayList.cartArraylist.get(d).getPrice()));
Log.d("TOTAL:", String.valueOf(CartArrayList.cartArraylist.get(d).getTotal()));
Log.d("SUM:", String.valueOf(subTotal));
}
}
Use the following code
_ListAdapter.notifyDataSetChanged();
I am unsure based on your first sentence, because you said there that you add an item before you go to the CartActivity. I kind of doubt that, or you have a concurrency issue there.
If your first sentence is true, your onClick which adds the cart entry is probably called after your onCreate method runs, that means you only see it if you open the CartActivity the second time.
The biggest question is the timing of your add call.
The value won't reflect automatically. You have to call adapter.notifyDataSetChanged(); after updating your dataset.
来源:https://stackoverflow.com/questions/30166412/how-to-call-adapter-class-before-activity