Я пытаюсь удалить объект из расширяемого вида списка. Этот код находится в моем классе адаптера.Удаление элемента из корзины покупок
deleteIB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ShoppingCartEntry catalog = ShoppingCartHelper.getByProduct(cat);
Product selectedProduct = catalog.getProduct();
selectedProduct = null;
_cartList.remove(groupPosition);
ProductAdapter.this.notifyDataSetChanged();
}
});
Когда я делаю это он удалит элемент из ListView, но однажды открыть телеги деятельности снова, я могу найти дополнительную деталь в корзине. почему он так себя ведет? Нужно ли мне что-либо реализовать в своем классе активности.
ShoppingCartHelper
public static final String PRODUCT_INDEX = "PRODUCT_INDEX";
private static Map<Product, ShoppingCartEntry> cartMap = new HashMap<Product, ShoppingCartEntry>();
public static void setQuantity(Product product, int quantity) {
// Get the current cart entry
ShoppingCartEntry curEntry = cartMap.get(product);
// If the quantity is zero or less, remove the products
if (quantity <= 0) {
if (curEntry != null)
removeProduct(product);
return;
}
// If a current cart entry doesn't exist, create one
if (curEntry == null) {
curEntry = new ShoppingCartEntry(product, quantity);
cartMap.put(product, curEntry);
return;
}
// Update the quantity
curEntry.setQuantity(quantity);
}
public static int getProductQuantity(Product product) {
// Get the current cart entry
ShoppingCartEntry curEntry = cartMap.get(product);
if (curEntry != null)
return curEntry.getQuantity();
return 0;
}
public static void removeProduct(Product product) {
cartMap.remove(product);
}
public static List<Product> getCartList() {
List<Product> cartList = new Vector<Product>(cartMap.keySet().size());
for (Product p : cartMap.keySet()) {
cartList.add(p);
}
return cartList;
}
public static ShoppingCartEntry getByProduct(Product product) {
return cartMap.get(product);
}
}
ShoppingCartEntry
private Product mProduct;
private int mQuantity;
public ShoppingCartEntry(Product product, int quantity) {
mProduct = product;
mQuantity = quantity;
}
public Product getProduct() {
return mProduct;
}
public int getQuantity() {
return mQuantity;
}
public void setQuantity(int quantity) {
mQuantity = quantity;
}
}
Где вы храпите предметы повозки? – Blackbelt
selectedProduct = null бесполезен в этом контексте, вам, вероятно, нужно что-то вроде catalog.removeProduct() – Egor
@Egor Я получил это сообщение об ошибке, метод removeProduct() не определен для типа ShoppingCartEntry – modabeckham