I am using ArrayList<HashMap<String, String>> to store my cart items. Each time item is added to cart, item details are set to ArrayList and then calls setCart() method.
I want if items in ArrayList already exist then by calling setCart() it does not overwrite previous cart items.Instead it appends the ArrayList to the end of already existing ArrayList. I used ArrayList.add() method but it works only for HashMap<String,String> not working for ArrayList<HashMap<String, String>>
public class AddtoCart extends Application {
ArrayList<HashMap<String, String>> cart;
public void setCart(ArrayList<HashMap<String, String>> data) {
cart = data;
}
public ArrayList<HashMap<String, String>> getCart() {
return cart;
}
public int getSize() {
return cart.size();
}
}
Method called on addtocart button which is setting ArrayList in AddtoCart
private OnClickListener Cart = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
cart = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_PID, pid);
map.put(TAG_NAME, name);
map.put(TAG_CATEGORY, category);
map.put(TAG_PRICE, price);
map.put(TAG_IMAGE,String.valueOf(resId));
cart.add(map);
AddtoCart obj = (AddtoCart) getApplicationContext();
obj.setCart(cart);
Intent a = new Intent("com.example.pb.VIEWCART");
startActivity(a);
}
};
}
ArrayList.addAll