在Android中获取列表位置时出错

时间:2017-01-11 06:07:52

标签: android listview

 View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.img_smc_add:
                    int addPos = Integer.parseInt("" + v.getTag());
                    int addQty = Integer.parseInt("" + mArrListMyCart.get(addPos).getmStrQty());
                    addQty++;
                    mArrListMyCart.get(addPos).setmStrQty("" + addQty);
                    myCartAdapter.notifyDataSetChanged();
                    mJsonObject = new JSONObject();
                    JSONObject option = new JSONObject();
                    try {
                        mJsonObject.put("productid", mArrListMyCart.get(addPos).getmStrProductId());
                        mJsonObject.put("itemid", mArrListMyCart.get(addPos).getmStrItemId());
                        mJsonObject.put("qty", mArrListMyCart.get(addPos).getmStrQty());
                        mJsonObject.put("shoppingcartid", shoppingcartid);
                        mJsonObject.put("customerid", "" + SessionClass.getUserId(getActivity()));

                        // option.put("options", "");
                        mJsonObject.put("options", "" + option);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    editCartDetail();

                    myCartAdapter.notifyDataSetChanged();
                    //calculateGrandTotal();
                    break;
                case R.id.img_smc_remove:
                    int removePos = Integer.parseInt("" + v.getTag());
                    int removeQty = Integer.parseInt("" + mArrListMyCart.get(removePos).getmStrQty());
                    if (removeQty > 1) {
                        removeQty--;
                        mArrListMyCart.get(removePos).setmStrQty("" + removeQty);
                        myCartAdapter.notifyDataSetChanged();
                    } else {
                        removeQty = 1;
                        mArrListMyCart.get(removePos).setmStrQty("" + removeQty);
                        myCartAdapter.notifyDataSetChanged();
                    }
                    myCartAdapter.notifyDataSetChanged();
                    mJsonObject = new JSONObject();
                    option = new JSONObject();
                    try {
                        mJsonObject.put("productid", mArrListMyCart.get(removePos).getmStrProductId());
                        mJsonObject.put("itemid", mArrListMyCart.get(removePos).getmStrItemId());
                        mJsonObject.put("qty", mArrListMyCart.get(removePos).getmStrQty());
                        mJsonObject.put("shoppingcartid", "" + shoppingcartid);
                        mJsonObject.put("customerid", "" + SessionClass.getUserId(getActivity()));
                        Log.e("customerid", "" + SessionClass.getUserId(getActivity()));
                        // option.put("options", "");
                        mJsonObject.put("options", "" + option);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    editCartDetail();
                    //getMyCartItems();
                    myCartAdapter.notifyDataSetChanged();
                    // calculateGrandTotal();
                    break;

                case R.id.iv_edit_cart_item:
                    FragmentManager fm=getActivity().getSupportFragmentManager();
                    Fragment fragment = new ProductDetailActivity();


                    int updatePos = v.getTag() != null ? Integer.parseInt("" + v.getTag()):0;

                    Toast.makeText(getActivity(),"updatePos:"+ updatePos,Toast.LENGTH_SHORT).show();
                    Bundle bundle = new Bundle();
                    bundle.putString("key","update");
                    bundle.putString("product_id", mArrListMyCart.get(updatePos).getmStrProductId());
                    bundle.putString("item_id", mArrListMyCart.get(updatePos).getmStrItemId());
                    fragment.setArguments(bundle);
                    // showAlert("Product id-->" + mArrListMyCart.get(Integer.parseInt(""+v.getTag())).getmStrProductId(), -1);
                    fm.beginTransaction().replace(R.id.content_frame, fragment).addToBackStack("").commit();
                    break;

                case R.id.iv_remove_cart_item:

                    int cartPos = Integer.parseInt("" + v.getTag());
                    int cartQty = Integer.parseInt("" + mArrListMyCart.get(cartPos).getmStrQty());
                    item_id = mArrListMyCart.get(cartPos).getmStrItemId();
                    product_id = mArrListMyCart.get(cartPos).getmStrProductId();
                    getDeleteCartItems();
                    mArrListMyCart.remove(cartPos);
                    //getMyCartItems();
                    myCartAdapter.notifyDataSetChanged();
                    if (cartQty > 1) {
                        mArrListMyCart.remove(cartPos);
                        myCartAdapter.notifyDataSetChanged();
                    } else {
                        removeQty = 1;

                    }


                    //calculateGrandTotal();
                    break;




                case R.id.tv_show_details:
                    int pos = Integer.parseInt("" + v.getTag());
                    item_id = mArrListMyCart.get(pos).getmStrItemId();
                    product_id = mArrListMyCart.get(pos).getmStrProductId();
                    Toast.makeText(getActivity(),"updatePos:"+ pos,Toast.LENGTH_SHORT).show();
                    getCartItemDetail();
                    break;


            }
        }
    };

我根据产品列表创建了listview。当我使用下面的行时,

 `int updatePos = v.getTag() != null ? Integer.parseInt("" + v.getTag()):0;`

updatepos总是为null.so如果我选择例如[3]产品但是它总是显示[0]产品详细信息以解决它?。updatePos不会根据列表中的选项更新更新。是0.我能做什么?

0 个答案:

没有答案