How to Call Adapter class before Activity

你离开我真会死。 提交于 2019-12-01 17:55:56

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!