单击ListView中的上一个列表项时,最后一个值会递增/递减

时间:2016-03-16 09:22:52

标签: android listview baseadapter

我正在开发一款类似于购物车的应用。在这里,我使用BaseAdapter来填充购物车数据。有,

3 ListItems:

第1行

第2行

第3行

当我点击第1行/第2行的增加/减少按钮时,第3行的购物车项目数分别增加/减少。我需要计算所有产品的TotalSum。

这是我的代码。请检查并帮我修复它。

提前致谢。

public class CartCountBaseAdapter extends BaseAdapter implements View.OnClickListener {
    Context con;
    ArrayList<HashMap<String, String>> ArproductMap;
    String MYFRAGMENT;
    ViewHolder viewHolder;


    public CartCountBaseAdapter(Context context, ArrayList<HashMap<String, String>> arproductMap, String myfragment) {
        this.con = context;
        this.ArproductMap = arproductMap;
        this.MYFRAGMENT = myfragment;
    }

    @Override
    public int getCount() {
        return ArproductMap.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        viewHolder = new ViewHolder();
        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater) con.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.cart_list_item, null);
        }


        viewHolder.addTowish = (Button) convertView.findViewById(R.id.addTowish);
        viewHolder.remove = (Button) convertView.findViewById(R.id.remove);

        viewHolder.cartProduct = (TextView) convertView.findViewById(R.id.cartProduct);
        viewHolder.cartQuantity = (TextView) convertView.findViewById(R.id.cartQuantity);
        viewHolder.cartCount = (TextView) convertView.findViewById(R.id.cartCount);
        viewHolder.cartPrice = (TextView) convertView.findViewById(R.id.cartPrice);
        viewHolder.cartPriceDum = (TextView) convertView.findViewById(R.id.cartPriceDum);

        viewHolder.ivDecrease = (ImageView) convertView.findViewById(R.id.ivDecrease);
        viewHolder.ivIncrease = (ImageView) convertView.findViewById(R.id.ivIncrease);

        viewHolder.cardView = (CardView) convertView.findViewById(R.id.cardlist_item);

        if (MYFRAGMENT == "CheckOutFragment") {
            viewHolder.addTowish = (Button) convertView.findViewById(R.id.addTowish);
            viewHolder.remove = (Button) convertView.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);
        }
        int CARTPRICE = Integer.parseInt(ArproductMap.get(position).get("Hash_Cart_Price"));
        viewHolder.cartProduct.setText(ArproductMap.get(position).get("Hash_Product_Name"));
        viewHolder.cartQuantity.setText(ArproductMap.get(position).get("Hash_Cart_Category"));
        viewHolder.cartCount.setText(ArproductMap.get(position).get("Hash_Cart_Quantity"));
        viewHolder.cartPrice.setText(String.valueOf(CARTPRICE));
        viewHolder.cartPriceDum.setText(String.valueOf(CARTPRICE));

        viewHolder.Quantity = Integer.parseInt(viewHolder.cartCount.getText().toString());

        viewHolder.addTowish.setOnClickListener(this);
        viewHolder.remove.setOnClickListener(this);

        viewHolder.ivDecrease.setTag(position);
        viewHolder.ivIncrease.setTag(position);

        viewHolder.ivDecrease.setOnClickListener(this);
        viewHolder.ivIncrease.setOnClickListener(this);


        return convertView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivIncrease:
                viewHolder.Quantity++;
                viewHolder.cartCount.setText(viewHolder.Quantity + "");

                break;

            case R.id.ivDecrease:
                viewHolder.Quantity--;
                if (viewHolder.Quantity <= 0) {
                    viewHolder.Quantity = 0;
                    viewHolder.cartCount.setText(viewHolder.Quantity + "");
                } else {
                    viewHolder.cartCount.setText(viewHolder.Quantity + "");
                }
                break;
        }
    }

    static class ViewHolder {
        TextView cartProduct,
                cartQuantity,
                cartCount,
                cartPrice,
                cartPriceDum;

        int Quantity;
        ImageView ivDecrease;
        ImageView ivIncrease;
        Button addTowish;
        Button remove;
        CardView cardView;
    }
}

1 个答案:

答案 0 :(得分:0)

您绝不应将任何数据放入视图持有者中。将数据放入任何数据类型,如Arraylist。然后获取您点击的位置并根据该位置修改数据

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.ivIncrease:
            int position  = (int) v.getTag
            datalist[position]++;
            viewHolder.cartCount.setText(viewHolder.Quantity + "");

            break;

        case R.id.ivDecrease:
            int position  = (int) v.getTag
            datalist[position]++;
            if (viewHolder.Quantity <= 0) {
                viewHolder.Quantity = 0;
                viewHolder.cartCount.setText(viewHolder.Quantity + "");
            } else {
                viewHolder.cartCount.setText(viewHolder.Quantity + "");
            }
            break;
    }
}

此外,如果您想进行任何与视图相关的更改,请将您的onclick界面放在viewholder类中,然后使用此对象