2017-11-27 1 views
0

특정 조건이 Android 프로그래밍에서 충족 된 후 내 정수 변수를 증가시키는 데 문제가 있습니다. 왜 증가하지 않는거야? 도와주세요 ...정수 변수를 증가 시키시겠습니까?

아래 코드는 Product라는 클래스 (Activity)에있는 ImageButton에서 구현됩니다.

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 Product\nQuantity 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; 
    } 
} 
+0

증가하지 않은 변수 :

그래서이 추가 증가 하는가? – diegoveloper

답변

0

+ = 카운트; 카운트 ++로 시도해보십시오.

0

카운트 ++는 코드에서 count = count + 1과 같지만 count + = count가 아닙니다. Count + = count는 count의 값 위에 count의 양을 더합니다.

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

관련 문제