1

I am creating Shopping Cart application and getting Image, Name, Price, Qty and Total of each and every Item, but not getting SubTotal of all the list items for the first time, and when I call Cart activity again then getting Sub Total of all the Cart Items.

NOTE: The issue is activity calls first then an adapter therefore i am not getting Total for the first time and i am setting value for the total in an adapter.

CartActivity's whole code check here

CartAdapter's whole code check here

CartArrayList.java:

public class CartArrayList {

    static ArrayList<Cart> cartArraylist = new ArrayList<Cart>();

}

CartActivity.java

for(int d=0; d<CartArrayList.cartArraylist.size(); d++) {

Log.d("ITEM-NAME:", CartArrayList.cartArraylist.get(d).getName().toString());

Log.d("ITEM-QUANTITY:", String.valueOf(CartArrayList.cartArraylist.get(d).getQuantity()));

Log.d("ITEM-PRICE:", String.valueOf(CartArrayList.cartArraylist.get(d).getPrice()));

Log.d("ITEM-TOTAL:", String.valueOf(CartArrayList.cartArraylist.get(d).getTotal()));

subTotal = subTotal + CartArrayList.cartArraylist.get(d).getTotal();

Log.d("SUB TOTAL:", String.valueOf(subTotal));

}

DecimalFormat decimalFormat = new DecimalFormat("0.00");

textSubTotal.setText(getResources().getString(R.string.Rs)+" "+decimalFormat.format(subTotal));

}

CartAdapter.java:

        df = new DecimalFormat("0.00##");

        totalPrice = cart.getQuantity() * cart.getPrice();
        Log.d("Total:Adapter", String.valueOf(totalPrice));
        CartArrayList.cartArraylist.get(position).setTotal(totalPrice);
        holder.textViewTotal.setText(context.getResources().getString(R.string.Rs)+" "+df.format(cart.getTotal()));     

My Question is Why I am not getting value for :

Log.d("ITEM-TOTAL:", String.valueOf(CartArrayList.cartArraylist.get(d).getTotal()));

Logcat

11-27 13:03:16.518: D/ITEM-NAME:(4176): Item 1
11-27 13:03:16.518: D/ITEM-QUANTITY:(4176): 1
11-27 13:03:16.518: D/ITEM-PRICE:(4176): 999.0
11-27 13:03:16.518: D/ITEM-TOTAL:(4176): 0.0
11-27 13:03:16.518: D/SUB TOTAL:(4176): 0.0

11-27 13:03:16.518: D/ITEM-NAME:(4176): Item 2
11-27 13:03:16.518: D/ITEM-QUANTITY:(4176): 1
11-27 13:03:16.518: D/ITEM-PRICE:(4176): 1900.0
11-27 13:03:16.518: D/ITEM-TOTAL:(4176): 0.0
11-27 13:03:16.518: D/SUB TOTAL:(4176): 0.0

11-27 13:03:16.598: D/Total:Adapter(4176): 999.0
11-27 13:03:16.608: D/Total:Adapter(4176): 1900.0
Sophie
  • 2,594
  • 10
  • 41
  • 75

1 Answers1

0

@Sophie you are getting total into adapter because you are doing this

 totalPrice = cart.getQuantity() * cart.getPrice();
    Log.d("Total:Adapter", String.valueOf(totalPrice));
    CartArrayList.cartArraylist.get(position).setTotal(totalPrice);

--> you are setting total price and then you are getting it so first time you are setting total value or not please check

 for(int d=0; d<CartArrayList.cartArraylist.size(); d++) {

        Log.d("ITEM-NAME:", CartArrayList.cartArraylist.get(d).getName().toString());

        Log.d("ITEM-QUANTITY:", String.valueOf(CartArrayList.cartArraylist.get(d).getQuantity()));

        Log.d("ITEM-PRICE:", String.valueOf(CartArrayList.cartArraylist.get(d).getPrice()));

        Log.d("ITEM-TOTAL:", String.valueOf(CartArrayList.cartArraylist.get(d).getTotal()));
CartArrayList.cartArraylist.setTotal((CartArrayList.cartArraylist.get(d).getQuantity())*(CartArrayList.cartArraylist.get(d).getPrice()));
        subTotal = subTotal + CartArrayList.cartArraylist.get(d).getTotal();

        Log.d("SUB TOTAL:", String.valueOf(subTotal));

    }

--> post your code again if it is still not working :) thanks

Hardy
  • 2,576
  • 1
  • 23
  • 45
  • yes i know this is the only question how can i get value for the very first time, I have posted my whole code already – Sophie Nov 27 '15 at 08:35
  • @Sophie try this code at first you will get your total there – Hardy Nov 27 '15 at 08:38
  • but my concern is why I am not getting value CartArrayList.cartArraylist.get(d).getTotal()) in onCreate – Sophie Nov 27 '15 at 08:45
  • @Sophie because you are not setting your value to set total at first time which you are doing in adapter. – Hardy Nov 27 '15 at 08:45