2016-03-01 56 views
0

我使用listview来填充数据。一切都很好,直到滚动它。滚动所有文本视图(COUNT)时将更改为默认值。在这里,我使用光标适配器来填充它。TextView的值在滚动列表视图时发生变化

enter image description here

当我点击^/落于下项值发生变化的项目箭头

NEXT ERROR:

当我点击REMOVE按钮,而不是删除我点击的项目,其他项目从列表中删除。

这里是我的AdapterClass:

public class CartCursorAdapter extends CursorAdapter implements View.OnClickListener { 

    public static final String MyPREFERENCES = "Preference"; 
    public ViewHolder viewHolder; 
    DbUtil dbUtil; 
    LayoutInflater mInflater; 
    Context contextNew; 
    String MY_FRAGEMNT; 
    SharedPreferences pref; 
    String strItemName, strCategoryName, strPrice, strCartCount; 
    int price = 0; 
    String quantity; 
    int total; 
    String netA = "0"; 
    int sum = 0; 
    float netAmount; 
    int cartPrice, cartSum, cartCount; 

    public CartCursorAdapter(Context context, Cursor c, String MYFRAGMENT, boolean autoRequery) { 
     super(context, c, autoRequery); 
     mInflater = LayoutInflater.from(context); 
     contextNew = context; 
     MY_FRAGEMNT = MYFRAGMENT; 
     dbUtil = new DbUtil(context); 
     dbUtil.open(); 
     pref = context.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE); 
     strItemName = pref.getString("itemName", null); 
     strCategoryName = pref.getString("categoryName", null); 
     strPrice = pref.getString("Price", null); 
     strCartCount = pref.getString("cartCount", null); 
    } 

    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     View v = mInflater.inflate(R.layout.cart_list_item, null); 
     return v; 
    } 

    @Override 
    public void bindView(View itemView, Context context, Cursor cursor) { 
     viewHolder = (ViewHolder) itemView.getTag(); 
     if (viewHolder == null) { 

      viewHolder = new ViewHolder(); 
      viewHolder.cartProduct = (TextView) itemView.findViewById(R.id.cartProduct); 
      viewHolder.cartQuantity = (TextView) itemView.findViewById(R.id.cartQuantity); 
      viewHolder.cartPrice = (TextView) itemView.findViewById(R.id.cartPrice); 
      viewHolder.cartPriceDum = (TextView) itemView.findViewById(R.id.cartPriceDum); 
      viewHolder.cartCount = (TextView) itemView.findViewById(R.id.cartCount); 
      viewHolder.ivDecrease = (ImageView) itemView.findViewById(R.id.ivDecrease); 
      viewHolder.ivIncrease = (ImageView) itemView.findViewById(R.id.ivIncrease); 
      viewHolder.addTowish = (Button) itemView.findViewById(R.id.addTowish); 
      viewHolder.remove = (Button) itemView.findViewById(R.id.remove); 
      viewHolder.cardView = (CardView) itemView.findViewById(R.id.cardlist_item); 
      viewHolder.ivDecrease.setOnClickListener(this); 
      viewHolder.ivIncrease.setOnClickListener(this); 
      viewHolder.addTowish.setOnClickListener(this); 
      viewHolder.remove.setOnClickListener(this); 
      itemView.setTag(viewHolder); 

     } 

     if (MY_FRAGEMNT == "CheckOutFragment") { 
      viewHolder.addTowish = (Button) itemView.findViewById(R.id.addTowish); 
      viewHolder.remove = (Button) itemView.findViewById(R.id.remove); 
      viewHolder.addTowish.setVisibility(View.GONE); 
      viewHolder.remove.setVisibility(View.GONE); 
      viewHolder.cardView.setCardElevation(0); 
      LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams 
        (LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
      viewHolder.cardView.setLayoutParams(layoutParams); 
      itemView.setTag(viewHolder); 
     } 

     String pdtName = cursor.getString(cursor.getColumnIndex(DbHelper.CART_PDT_NAME)); 
     String catName = cursor.getString(cursor.getColumnIndex(DbHelper.CART_CAT_NAME)); 
     int cartprice = cursor.getInt(cursor.getColumnIndex(DbHelper.CART_PRICE)); 
     int strquantity = cursor.getInt(cursor.getColumnIndex(DbHelper.CART_QUANTITY)); 
     viewHolder.cartProduct.setText(pdtName); 
     viewHolder.cartPrice.setText(String.valueOf(cartprice)); 
     viewHolder.cartQuantity.setText(catName); 
     viewHolder.cartCount.setText(String.valueOf(strquantity)); 
    } 

    @Override 
    public void onClick(View view) { 
     switch (view.getId()) { 
      case R.id.remove: 
       dbUtil.open(); 
       String delItem = viewHolder.cartProduct.getText().toString(); 
       Cursor Cartcursor = dbUtil.getCartID(delItem); 
       if (Cartcursor != null && Cartcursor.moveToFirst()) { 
        Cartcursor.moveToFirst(); 
        String strCartProductID = Cartcursor.getString(Cartcursor.getColumnIndex(DbHelper.CART_PDT_ID)); 
        dbUtil.deleteCart(strCartProductID, delItem); 
        Toast.makeText(contextNew, "Cart Item " + "RowId" + strCartProductID + " Product Id" + delItem, Toast.LENGTH_SHORT).show(); 
        Toast.makeText(contextNew, "Deleted Successfully", Toast.LENGTH_SHORT).show(); 
       } 
       break; 

      case R.id.addTowish: 
       break; 
      case R.id.ivIncrease: 
       increase(); 
       break; 
      case R.id.ivDecrease: 
       decrease(); 
       break; 

     } 
    } 

    private void decrease() { 
     strPrice = viewHolder.cartPrice.getText().toString(); 
     price = Integer.parseInt(strPrice); 
     int counter = 0; 
     try { 
      counter = Integer.parseInt(viewHolder.cartProduct.getText().toString()); 
     } catch (NumberFormatException e) { 
      e.printStackTrace(); 
     } 
     counter--; 

     if (counter > 0) { 
      viewHolder.cartProduct.setText(Integer.toString(counter)); 
      viewHolder.cartPrice.setVisibility(View.GONE); 
      viewHolder.cartPriceDum.setVisibility(View.VISIBLE); 
      quantity = viewHolder.cartProduct.getText().toString(); 
      total = (Integer.parseInt(quantity)) * (price); 
      netA = String.valueOf(total); 
      sum -= price; 
      netAmount = sum; 

      viewHolder.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(); 
      } 
     } 
    } 

    private void increase() { 
     strPrice = viewHolder.cartPrice.getText().toString(); 
     price = Integer.parseInt(strPrice); 
     int counter = 0; 
     try { 
      counter = Integer.parseInt(viewHolder.cartCount.getText().toString()); 
     } catch (NumberFormatException e) { 
      e.printStackTrace(); 
     } 
     counter++; 

     if (counter > 0) { 
      viewHolder.cartCount.setText(Integer.toString(counter)); 
      viewHolder.cartPrice.setVisibility(View.GONE); 
      viewHolder.cartPriceDum.setVisibility(View.VISIBLE); 
      quantity = viewHolder.cartCount.getText().toString(); 

      total = (Integer.parseInt(quantity)) * (price); 
      netA = String.valueOf(total); 
      sum += price; 
      netAmount = sum; 

      viewHolder.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; 
      } 
     } 
    } 

    public static class ViewHolder { 
     public static Button addTowish, remove; 
     public TextView cartProduct, cartQuantity, cartPrice, cartCount, cartPriceDum; 
     public ImageView ivDecrease, ivIncrease; 
     CardView cardView; 
    } 
} 
+0

我已经将所有值存储到'List '帮助我来修复我的错误。 –

回答

0

对于REMOVE: 创建自定义OnClickListener这需要cartProduct作为参数

public class CustomOnClick implements OnClickListener { 
    String product; 


    public CustomOnClick(String product) { 
     super(); 
     this.product = product; 
    } 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     dbUtil.open(); 
      String delItem = product; //set product to delItem 
      Cursor Cartcursor = dbUtil.getCartID(delItem); 
      if (Cartcursor != null && Cartcursor.moveToFirst()) { 
       Cartcursor.moveToFirst(); 
       String strCartProductID = Cartcursor.getString(Cartcursor.getColumnIndex(DbHelper.CART_PDT_ID)); 
       dbUtil.deleteCart(strCartProductID, delItem); 
       Toast.makeText(contextNew, "Cart Item " + "RowId" + strCartProductID + " Product Id" + delItem, Toast.LENGTH_SHORT).show(); 
       Toast.makeText(contextNew, "Deleted Successfully", Toast.LENGTH_SHORT).show(); 
      } 
    } 
} 

然后同时设定onclicklistener删除按钮,你可以尝试

viewHolder.remove.setOnClickListener(new CustomOnClick(cursor.getString(cursor.getColumnIndex(DbHelper.CART_PDT_NAME)))); 

并用于存储COUN T值,当你加载列表视图在HashMap中添加值(它存储产品名称和计数对它)后,每当你增加或减少COUNT只是更新其在散列表中的值

相关问题