增加整数变量



我在Android编程中满足特定条件后会遇到问题。为什么不增加?

下面的代码是在映像键盘上实现的,该类别(活动)称为product。

int count = 1;
List<Item> prod = ShoppingCart.getInstance().getProducts();
// if arraylist is null --> add a product
if(prod != null && prod.isEmpty()){
    ShoppingCart.getInstance().addItem(new Item(
            namePureString,
            manufacturePureString,
            pricePureString,
            "",
            count
    ));
    Toast.makeText(getApplicationContext(), namePureString + " Added To Cart", Toast.LENGTH_SHORT).show();
}else {
    for (Item products : ShoppingCart.getInstance().myProducts) {
        // here i am checking if the product exists, if it does --> count has to increment
        if (products.getManufacture().equals(manufacturePureString)) {
            count += count;
            Toast.makeText(getApplicationContext(), "You Added This ProductnQuantity Will Increase",
                    Toast.LENGTH_SHORT).show();
            ShoppingCart.getInstance().setItemExists(new Item(
                    namePureString,
                    manufacturePureString,
                    pricePureString,
                    "",
                    count
            ));
            } else {
            ShoppingCart.getInstance().addItem(new Item(
                    namePureString,
                    manufacturePureString,
                    pricePureString,
                    "",
                    count
            ));
        }
}

这是我的单身班,我称之为其方法:

public class ShoppingCart {
    private static final String TAG = "Products: ";
    private static ShoppingCart ourInstance = null;
    public ArrayList<Item> myProducts = new ArrayList<>();
    private ShoppingCart() {
    }
    public static ShoppingCart getInstance() {
        // if object instance does not exist create a new one and use that one only
        if ( ourInstance == null){
            ourInstance = new ShoppingCart();
        }
        return ourInstance;
    }
    public void addItem(Item item){
            ourInstance.myProducts.add(item);
    }
    public void setItemExists(Item item) {
        int itemIndex = ourInstance.myProducts.indexOf(item);
        if (itemIndex != -1) {
            ourInstance.myProducts.set(itemIndex, item);
        }
    }
    public List<Item> getProducts(){
        return this.myProducts;
    }
}

count 与count = count 1相同,而不是计数 =计数中的计数。count =计数添加了数量的数量。

的数值。

因此,要添加以下添加:

count = count + 1; //or count++;

尝试

int count = 0;
count = count + 1;
Log.d("Answer: ",count);

而不是count = count;尝试使用Count ;

尝试

相关内容

  • 没有找到相关文章

最新更新