2016-03-03 80 views
0

我使用游标适配器将购物车项目填充到列表中。我的ListView项具有购物车计数增加和减少,同时点击按钮

  1. 价格的TextView
  2. 产品名称的TextView
  3. 计数的TextView
  4. 增量按钮
  5. 递减按钮移到

当我点击递增/递减键的值在一定的条件下递增/递减后接触查看行,否则计数不会改变。 在某些情况下,如果我单击第1行增量按钮,则项目计数在第2行中增加。

注意: 1.我使用光标适配器填充List。 2.使用增量&在活动类中减少clicklistener。

这里是我的代码:

cartList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
    @Override 
    public void onItemClick(final AdapterView<?> parent, View itemView, final int position, final long rowId) { 

     txt_cartProduct = (TextView) itemView.findViewById(R.id.cartProduct); 
     txt_cartQuantity = (TextView) itemView.findViewById(R.id.cartQuantity); 
     txt_cartPrice = (TextView) itemView.findViewById(R.id.cartPrice); 
     txt_cartPriceDum = (TextView) itemView.findViewById(R.id.cartPriceDum); 
     txt_cartCount = (TextView) itemView.findViewById(R.id.cartCount); 
     img_ivDecrease = (ImageView) itemView.findViewById(R.id.ivDecrease); 
     img_ivIncrease = (ImageView) itemView.findViewById(R.id.ivIncrease); 
     but_addTowish = (Button) itemView.findViewById(R.id.addTowish); 
     but_remove = (Button) itemView.findViewById(R.id.remove); 

     img_ivIncrease.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       strPrice = txt_cartPrice.getText().toString(); 
       price = Integer.parseInt(strPrice); 
       int counter = 0; 
       try { 
        counter = Integer.parseInt(txt_cartCount.getText().toString()); 
       } catch (NumberFormatException e) { 
        e.printStackTrace(); 
       } 
       counter++; 

       if (counter > 0) { 
        txt_cartCount.setText(Integer.toString(counter)); 
        txt_cartPrice.setVisibility(View.GONE); 
        txt_cartPriceDum.setVisibility(View.VISIBLE); 
        quantity = txt_cartCount.getText().toString(); 

        total = (Integer.parseInt(quantity)) * (price); 
        netA = String.valueOf(total); 
        sum += price; 
        if (sum == 0) { 
         netAmount = Integer.parseInt(txt_cartPriceDum.getText().toString()); 
        } 
        netAmount = sum; 
        Log.e("Sum is", String.valueOf(sum)); 
        txt_cartPriceDum.setText(String.valueOf(total)); 
        cartCount = Integer.parseInt(quantity); 

        Toast.makeText(context, "netAmount" + netAmount + "\n" + "Total" + total, Toast.LENGTH_SHORT).show(); 
        if (counter == 1) { 
         cartPrice = price; 
         cartSum = sum; 
        } 

        if (counter == 0) { 
         cartPrice = 0; 
         cartSum = 0; 
         cartCount = 0; 
         Toast.makeText(context, "Minimum Item is 1", Toast.LENGTH_SHORT).show(); 
        } 
        int count_check = 1; 
        if (count_check >= position) { 
         count_check++; 
        } else if (count_check == 0) { 
         Toast.makeText(context, "Minimum Item is 1", Toast.LENGTH_SHORT).show(); 
        } 
       } 
      } 
     }); 


     img_ivDecrease.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       strPrice = txt_cartPrice.getText().toString(); 
       price = Integer.parseInt(strPrice); 
       int counter = 0; 
       try { 
        counter = Integer.parseInt(txt_cartCount.getText().toString()); 
       } catch (NumberFormatException e) { 
        e.printStackTrace(); 
       } 
       counter--; 

       if (counter > 0) { 
        txt_cartCount.setText(Integer.toString(counter)); 
        txt_cartPrice.setVisibility(View.GONE); 
        txt_cartPriceDum.setVisibility(View.VISIBLE); 
        quantity = txt_cartCount.getText().toString(); 
        total = (Integer.parseInt(quantity)) * (price); 
        netA = String.valueOf(total); 
        sum -= price; 
        if (sum == 0) { 
         netAmount = Integer.parseInt(txt_cartPriceDum.getText().toString()); 
        } 
        Log.e("Sum - is", String.valueOf(sum)); 
        txt_cartPriceDum.setText(String.valueOf(total)); 
        cartCount = Integer.parseInt(quantity); 
        Toast.makeText(context, "netAmount" + netAmount + "\n" + "Total" + total, Toast.LENGTH_SHORT).show(); 
        if (counter == 1) { 
         cartPrice = price; 
         cartSum = sum; 
        } 

        if (counter == 0) { 
         cartPrice = 0; 
         cartSum = 0; 
         cartCount = 0; 

        } 
       } 
      } 
     }); 
    } 
}); 

回答

1

您可以使用setTag()getTag()方法在每个列表项节能计数器值。

请参阅this answer了解更多详情。

相关问题