How to call adapter class before activity

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; } } 
+5
source share
5 answers

You need to calculate the total value in essence only before setting the list of arrays in the Adapter, and not calculate 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 this helps ツ

+3
source

You will need to move your list to onResume (), and it will be useful for you to save your list in outstate bundle onSaveInstanceState () and then check its existence to refill your list in onCreate () savedInstanceState bundle, This is indirectly related to your question; ) I know, but if you move the population and show your list on onResume (), you will also want to do these things so that you do not repeat the work that you have potentially already done.

Also, move the notifyDataSetChanged () file after the place where you are updating 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)); } } 
0
source

Use the following code

 _ListAdapter.notifyDataSetChanged(); 
0
source

I am sure, based on your first sentence, because you said you were adding an item before moving to CartActivity. I doubt you have a concurrency problem.

If your first sentence is true, your onClick, which adds the entry to the cart, is likely to be called after the onCreate method is run, which means that you only see it if you open CartActivity again.

The biggest question is the time of your additional call.

0
source

The value will not be displayed automatically. You should call adapter.notifyDataSetChanged (); after updating your dataset.

-2
source

All Articles