如何更新android中每个listview项的购物车数量?

时间:2015-05-20 14:54:01

标签: android

我想更新每个listview项目的购物车数量。当我滚动列表视图时,会自动更改购物车计数位置。当我点击购物车按钮对话框时会弹出数字。

@覆盖         public View getView(final int position,View convertView,ViewGroup parent){

        final ViewHolder holder;

        if (convertView == null) {
            mInflater = Currentactivity.getLayoutInflater();
            convertView = mInflater.inflate(R.layout.products_item, null);
            holder = new ViewHolder();

            holder.title = (TextView) convertView.findViewById(R.id.title);
            holder.desc = (TextView) convertView.findViewById(R.id.desc);
            holder.price = (TextView) convertView.findViewById(R.id.price);
            holder.items_count = (TextView) convertView
                    .findViewById(R.id.items_count);

            holder.product_image_imageview = (ImageView) convertView
                    .findViewById(R.id.product_image);
            holder.bottle_image = (ImageView) convertView
                    .findViewById(R.id.bottle_image_set);
            holder.product_cart = (FrameLayout) convertView
                    .findViewById(R.id.product_cart);

            holder.product_cart.setTag(holder.items_count);

            convertView.setTag(holder);
            // holder.checkbox.setTag(list.get(position));
        } else {
            holder = (ViewHolder) convertView.getTag();
            // holder.checkbox.setTag(list.get(position));
        }

        holder.product_cart.setTag(position);
        holder.title.setText(productArray.get(position).get("title"));
        holder.desc.setText(productArray.get(position).get("body_html"));
        holder.price.setText(Currentactivity.getResources().getString(
                R.string.rs)
                + "" + productArray.get(position).get("price"));

        holder.product_cart.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                int pos = (Integer) v.getTag();

                /*holder.items_count.setVisibility(View.VISIBLE);
                holder.items_count.setText(""+pos);
                */

                Log.i("pos", ""+pos);
                items = showCartNumber(pos, holder.items_count);


            }
        });

        // tviews.add(holder.items_count);



        items = "";

        try {`enter code here`
            imageLoader.displayImage(productArray.get(position).get("image"),
                    holder.product_image_imageview, options);

            // bottle_image.setImageResource(product_image_bottle[0]);

        } catch (Exception e) {

        }

        return convertView;
    }

    static class ViewHolder {
        TextView title, price, desc, items_count;
        ImageView product_image_imageview, bottle_image;
        FrameLayout product_cart;
    }

    protected String showCartNumber(final int position,
            final TextView items_count) {
        // TODO Auto-generated method stub

        final Dialog dialog = new Dialog(Currentactivity,
                android.R.style.Theme_Light);
        dialog.getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.cart_number_layout);
        dialog.setCanceledOnTouchOutside(false);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
        dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
        dialog.show();

        quantityet = (EditText) dialog.findViewById(R.id.quantityet);
        Button one = (Button) dialog.findViewById(R.id.one);

        Button star = (Button) dialog.findViewById(R.id.ok);
        Button zero = (Button) dialog.findViewById(R.id.zero);
        Button delete = (Button) dialog.findViewById(R.id.delete);



        star.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                final String value = quantityet.getText().toString();

                if (!(quantityet.getText().toString().trim().length() == 0)) {

                    if (!(value.equalsIgnoreCase("0"))) {


                        db.open();
                        Cursor c = db.checkCartExist(productArray.get(position)
                                .get("title"));
                        if (c.moveToFirst()) {

                            ToastUtils.displayToast(ToastTypeEnum.ALERT_TOAST,
                                    "Product already added to cart",
                                    Toast.LENGTH_SHORT);

                            /*db.updateProductCount(quantityet.getText()
                                    .toString(),productArray.get(position)
                                    .get("title"));*/

                        } else {

                            items_count.setVisibility(View.VISIBLE);
                            items_count.setText(value);


                            db.insertCartDetails(productArray.get(position)
                                    .get("title"), productArray.get(position)
                                    .get("price"), quantityet.getText()
                                    .toString(), productArray.get(position)
                                    .get("image"));

                            ToastUtils.displayToast(ToastTypeEnum.ALERT_TOAST,
                                    "Product added to cart",
                                    Toast.LENGTH_SHORT);


                        }
                        c.close();
                        db.close();

                        dialog.dismiss();
                    } else {
                        ToastUtils.displayToast(ToastTypeEnum.ALERT_TOAST,
                                "Please enter the valid quantity",
                                Toast.LENGTH_SHORT);
                    }

                } else {
                    ToastUtils.displayToast(ToastTypeEnum.ALERT_TOAST,
                            "Please enter the quantity", Toast.LENGTH_SHORT);
                }

            }
        });
        zero.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                addOrder(position, 0);
                // dialog.dismiss();
            }
        });
        delete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // addOrder(position,0);
                // dialog.dismiss();
                quantityet.setText("");
            }
        });

        return quantityet.getText().toString().trim();
    }

    private void addOrder(int position, int quantity) {

        if (quantityet.getText().toString().length() > 0) {
            quantityet.setText(quantityet.getText().toString().trim()
                    + String.valueOf(quantity));
        } else {
            quantityet.setText(String.valueOf(quantity));
        }
        quantityet.setSelection(quantityet.getText().length());

    }


}

感谢。

0 个答案:

没有答案