在android中更新我的购物车列表

时间:2015-06-10 05:40:40

标签: java android

我在Android中开发了购物车应用,我按照本教程http://www.androiddom.com/2011/06/android-shopping-cart-tutorial-part-2.html进行了操作 我现在面临一个问题。

当我设置一个项目的数量并点击setquantity时,它会将该项目添加到列表SHOPPING_CART_ACTIVITY,但如果我想立即更改数量,我应该点击相同的项目购物车,它会将我重定向到特定项目的页面PRODUCT_DETAILS_ACTIVITY

但是,如果我更改数量,则需要将其作为新数量。甚至旧的存在。

我检查了整个代码,我认为PRODUCT_DETAILS_ACTIVITY中出现了一个缺陷(每次都会创建一个新对象)。我尝试了notifyDataSetChanged,但没有用。

有人可以告诉我们可以做些什么吗?

shoppingcartactivity。(此处显示购物车中的最终产品列表,点击列表中的某个项目会将我重定向到该特定页面)

final ListView listViewCatalog = (ListView) findViewById(R.id.ListViewCatalog);
    mProductAdapter = new ProductAdapter(mCartList, getLayoutInflater(), true);
    listViewCatalog.setAdapter(mProductAdapter);

    listViewCatalog.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            Intent productDetailsIntent = new Intent(getBaseContext(),ProductDetailsActivity.class);
            productDetailsIntent.putExtra(ShoppingCartHelper.PRODUCT_INDEX, position);
            startActivity(productDetailsIntent);
        }
    });

}

@Override
protected void onResume() {
    super.onResume();

    // Refresh the data
    if(mProductAdapter != null) {
        mProductAdapter.notifyDataSetChanged();
    }
}

productdetailsactivity。每次当我点击购物车中的某个商品来更改数量时,它每次都会被视为新条目(因此它显示当前在购物车中为0),而且前一个数量项目(即旧项目也存在)。

    // 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;
}
}

productadapter。

@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;
}

0 个答案:

没有答案