5

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;
}

}
Sun
  • 6,768
  • 25
  • 76
  • 131
  • But why are you refreshing in `onCreate()` ? – M D May 11 '15 at 11:25
  • what does it mean ? How to refresh ArrayList – N J May 11 '15 at 11:25
  • use adapter.notifyDataSetChanged(); – Syed Raza Mehdi May 11 '15 at 11:26
  • 1
    You might want to try the onResume() method instead, when switching activities. http://stackoverflow.com/questions/9776059/refresh-oncreate-after-call-to-onresume – D. Visser May 11 '15 at 11:26
  • 2
    Oncreate() call only once instead use onResume() – Santosh Kathait May 11 '15 at 11:26
  • add it inside onStart() method – amodkanthe May 11 '15 at 11:27
  • You 'Must' Read about the [Android Activity Lifecycle](http://developer.android.com/training/basics/activity-lifecycle/index.html). – Skynet May 11 '15 at 11:29
  • I have tried it onResume() as well, but yet not done ... – Sun May 11 '15 at 11:38
  • 2
    I am searching for the question here. Can someone show me where the `?` is, I mean with that I should be able to find the sentence and with it the question by my own... still looking.... – WarrenFaith May 13 '15 at 11:50
  • @WarrenFaith may be i have not explained it well but my question is ? why its happening ! First time, I have added item to cart > switched to cart activity (not getting total) and Second time, I clicked on back and again from menu activity moved to cart activity (now getting total) – Sun May 13 '15 at 11:52
  • as others said move `adapter.notifyDataSetChanged();` on your onClick method, after `CartArrayList.cartArraylist.add(0, new Cart(itemsList.get(position).getTitle(), itemsList.get(position).getImage(), itemsList.get(position).getPrice()));` – Evripidis Drakos May 13 '15 at 11:55
  • @EvripidisDrakos I have tried using this as well, but did not get any success: CartArrayList.cartArraylist.add(0, new Cart(itemsList.get(position).getTitle(), itemsList.get(position).getPrice())); notifyDataSetChanged(); – Sun May 13 '15 at 11:58
  • that should be `adapter.notifyDataSetChanged();` wher `adapter` is the global variable you initiated on `onCreate()` – Evripidis Drakos May 13 '15 at 12:01
  • @EvripidisDrakos check my ViewPagerAdapter class code which extends PagerAdapter and here i have a button to add item to cart – Sun May 13 '15 at 12:03
  • I am waiting to see who will receive the [bounty](http://meta.stackexchange.com/questions/16065/how-does-the-bounty-system-work). – Maveňツ May 13 '15 at 12:06
  • I have posted ViewPagerAdapter class code, where i am using button to add item to Cart – Sun May 13 '15 at 12:10
  • Try to calculate total and set to each CartArrayList.cartArraylist item before start CartActivity. – Haresh Chhelana May 15 '15 at 05:53
  • cant u set total before setting adapter? why to calculate Total in `getView` ? just [like this](http://paste.ofcode.org/WaXmB7rFjLCCKpqqqRnVdB) – SweetWisher ツ May 15 '15 at 06:13

5 Answers5

3

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 ツ

SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74
0

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));
    }
}
Bill Mote
  • 12,644
  • 7
  • 58
  • 82
0

Use the following code

_ListAdapter.notifyDataSetChanged();
Piyush
  • 18,895
  • 5
  • 32
  • 63
rajlaxmi_jagdale
  • 1,370
  • 15
  • 16
0

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.

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
-2

The value won't reflect automatically. You have to call adapter.notifyDataSetChanged(); after updating your dataset.