Java 在android中更新我的购物车列表
我正在用Android开发一个购物车应用程序,我遵循了这个教程 我现在面临一个问题 当我设置一个项目的数量并单击Java 在android中更新我的购物车列表,java,android,Java,Android,我正在用Android开发一个购物车应用程序,我遵循了这个教程 我现在面临一个问题 当我设置一个项目的数量并单击setquantity,它会将该项目添加到列表购物车\活动,但如果我现在想更改数量,我应该单击购物车中的同一项目,它会将我重定向到特定项目的页面,产品\详细信息\活动 然而,如果我改变了数量,它将成为一个新的数量。甚至旧的也存在 我已经检查了整个代码,我认为产品详细信息\u活动中出现了一个缺陷(每次都会创建一个新对象)。我尝试了notifyDataSetChanged,但没有用 有人能
setquantity
,它会将该项目添加到列表购物车\活动
,但如果我现在想更改数量,我应该单击购物车中的同一项目,它会将我重定向到特定项目的页面,产品\详细信息\活动
然而,如果我改变了数量,它将成为一个新的数量。甚至旧的也存在
我已经检查了整个代码,我认为产品详细信息\u活动
中出现了一个缺陷(每次都会创建一个新对象)。我尝试了notifyDataSetChanged
,但没有用
有人能告诉我能做什么吗
shoppingcartactivity
(此处显示购物车中产品的最终列表,单击列表中的某个项目会将我重定向到该特定页面)
购物车帮手
public static void setQuantity(Product product, int quantity) {
// Get the current cart entry
ShoppingCartEntry curEntry = cartMap.get(product); gives null for the every entry
// 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;
}
}
代码片段不足以理解代码,能否发布ShoppingCartHelper.java代码。我已附上ShoppingCartHelper..似乎ShoppingCartHelper.java文件的代码正确。检查curEntry是否为null,因为在这种情况下,您在列表中添加了一个新条目。发布适配器代码。它可能在那里,因为Android上的列表重用UI对象,所以你得到了重复的内容。如果convertView不为null,则希望您重用它,而不是创建一个新的convertView。想看看它是什么样子吗?例如,我重用了它,但无法确定为什么每次curEntry都变为null。
// inside onCreate method....
LayoutInflater li;
int productIndex = getIntent().getExtras().getInt(
ShoppingCartHelper.PRODUCT_INDEX);
String PRODUCT_STRING="";
PRODUCT_STRING = getIntent().getExtras().getString("PRODUCT_STRING");
switch (PRODUCT_STRING)
{
case "Laptops":
// catalog = get laptop catalog
break;
case "Phones":
// catalog = get phones catalog
break;
}
final Product selectedProduct = (Product) catalog.get(productIndex);
Toast.makeText(getApplicationContext(),
"Inside selected product obj", Toast.LENGTH_SHORT).show();
productImageView = (ImageView) findViewById(R.id.ImageViewProduct);
productTitleTextView = (TextView) findViewById(R.id.TextViewProductTitle);
productDetailsTextView = (TextView) findViewById(R.id.TextViewProductDetails);
productImageView.setImageDrawable(selectedProduct.productImage);
productTitleTextView.setText(selectedProduct.title);
productDetailsTextView.setText(selectedProduct.description);
// Update the current quantity in the cart
TextView textViewCurrentQuantity = (TextView) findViewById(R.id.textViewCurrentlyInCart);
textViewCurrentQuantity.setText("Currently in Cart: "
+ ShoppingCartHelper.getProductQuantity(selectedProduct));
Toast.makeText(getApplicationContext(), "quantity "+ ShoppingCartHelper.getProductQuantity(selectedProduct),
Toast.LENGTH_SHORT).show();
// Save a reference to the quantity edit text
final EditText editTextQuantity = (EditText) findViewById(R.id.editTextQuantity);
Button setQuantity = (Button) findViewById(R.id.ButtonAddToCart);
setQuantity.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
quantity = Integer.parseInt(editTextQuantity.getText().toString());
ShoppingCartHelper.setQuantity(selectedProduct, quantity);
finish();
}
});
}
public static void setQuantity(Product product, int quantity) {
// Get the current cart entry
ShoppingCartEntry curEntry = cartMap.get(product); gives null for the every entry
// 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;
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewItem item;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item, null);
item = new ViewItem();
item.productImageView = (ImageView) convertView
.findViewById(R.id.ImageViewItem);
item.productTitle = (TextView) convertView
.findViewById(R.id.TextViewItem);
item.productQuantity = (TextView) convertView
.findViewById(R.id.textViewQuantity);
convertView.setTag(item);
} else {
item = (ViewItem) convertView.getTag();
}
Product curProduct = mProductList.get(position);
item.productImageView.setImageDrawable(curProduct.productImage);
item.productTitle.setText(curProduct.title);
// Show the quantity in the cart or not
if (mShowQuantity) {
item.productQuantity.setText("Quantity: "
+ ShoppingCartHelper.getProductQuantity(curProduct));
} else {
// Hid the view
item.productQuantity.setVisibility(View.GONE);
}
return convertView;
}
private class ViewItem {
ImageView productImageView;
TextView productTitle;
TextView productQuantity;
}