牛奶甜甜圈
2015-10-22 11:48
采纳率: 100%
浏览 2.1k

购物车listview加减按钮监听

android关于购物车listview加减按钮监听

我用vieholder按完加减跟新完数据后 更新数据是对的 但是跟新的itme不对 我刚觉viewholder记不住position的原因
想不用viewholder 但是太卡 我该怎么做 我是学渣一枚 求大神指导 程序都改蒙了 有不规范的 求大神指出来

 @Override
    public View getView(int position,View convertView, ViewGroup parent) {
        data=mlist.get(position);
        Log.i("wu", ""+position);
        //重新创建view  不做优化
//      View view=minflater.inflate(R.layout.buycar_thing_list,null);
//      ImageView buycar_image=(ImageView)view.findViewById(R.id.buycar_thingimage);
//      TextView thingname=(TextView) view.findViewById(R.id.buycar_thingname);
//      TextView thingvalue=(TextView) view.findViewById(R.id.bugcar_thingvalue);
//      TextView num=(TextView) view.findViewById(R.id.bugcar_thingnumber);
//      ImageView image_change=(ImageView) view.findViewById(R.id.buycar_sth_change);
//      ImageView image_reduce=(ImageView) view.findViewById(R.id.buycar_reduce);
//      TextView value_xiaoji=(TextView) view.findViewById(R.id.bugcar_thingvalue_two);
//      ImageView image_add=(ImageView)view.findViewById(R.id.buycar_add);
//      buycar_image.setImageResource(data.ImageResId);
//      thingname.setText(data.thingbuycar_name);
//      thingvalue.setText(data.thingbuycar_value);
//      buycar_image.setBackgroundResource(data.ImageResId);
//      thingname.setText(data.thingbuycar_name);
//      thingvalue.setText(data.thingbuycar_value);
//        num.setText(data.thingbuycar_num);
//      //选择判断
//              if(data.thingbuycar_or){
//                  image_change.setImageResource(R.drawable.pay_and_delivery_unchecked);
//                  value_xiaoji.setText("0.00");
//              }else{
//              double d_value=Double.parseDouble(data.thingbuycar_value);
//              double d_num=Double.parseDouble(data.thingbuycar_num);
//              double d_xiaoji=d_value*d_num/1.00;
//              DecimalFormat df = new DecimalFormat("0.00");
//                  String s=df.format(d_xiaoji);
//                value_xiaoji.setText(s);
//              }
//              if(data.thingbuycar_num!="1"){
//                  image_reduce.setImageResource(R.drawable.shopping_cart_product_num_reduce);
//              }
//              MyListener mylistener_add=new MyListener(position,1);
//          MyListener mylistener_reduce=new MyListener(position,2);
//          MyListener mylistener_change=new MyListener(position,3);
//             image_add.setOnClickListener(mylistener_add);
//          image_reduce.setOnClickListener(mylistener_reduce);
//          image_change.setOnClickListener(mylistener_change);
//      return view;
        MyListener mylistener_add=new MyListener(position,1);
        MyListener mylistener_reduce=new MyListener(position,2);
        MyListener mylistener_change=new MyListener(position,3);
        if(convertView==null){
            viewHolder=new viewHolder();
            vae=convertView=minflater.inflate(R.layout.buycar_thing_list, null);
            viewHolder.image=(ImageView) convertView.findViewById(R.id.buycar_thingimage);
           viewHolder.name=(TextView) convertView.findViewById(R.id.buycar_thingname);
            viewHolder.value=(TextView) convertView.findViewById(R.id.bugcar_thingvalue);
            viewHolder.num=(TextView) convertView.findViewById(R.id.bugcar_thingnumber);
            viewHolder.image_change=(ImageView) convertView.findViewById(R.id.buycar_sth_change);
            viewHolder.image_reduce=(ImageView) convertView.findViewById(R.id.buycar_reduce);
            viewHolder.value_xiaoji=(TextView) convertView.findViewById(R.id.bugcar_thingvalue_two);
            viewHolder.image_add=(ImageView)convertView.findViewById(R.id.buycar_add);
            convertView.setTag(viewHolder);
        }else{
            viewHolder=(viewHolder) convertView.getTag();
        }
        viewHolder.image.setBackgroundResource(data.ImageResId);
        viewHolder.name.setText(data.thingbuycar_name);
        viewHolder.value.setText(data.thingbuycar_value);
        viewHolder.num.setText(data.thingbuycar_num);
        Log.i("wu", "我就不信了"+position);
        //选择判断
        if(data.thingbuycar_or){
            viewHolder.image_change.setImageResource(R.drawable.pay_and_delivery_unchecked);
            viewHolder.value_xiaoji.setText("0.00");
        }else{
        double d_value=Double.parseDouble(data.thingbuycar_value);
        double d_num=Double.parseDouble(data.thingbuycar_num);
        double d_xiaoji=d_value*d_num/1.00;
        DecimalFormat df = new DecimalFormat("0.00");
            String s=df.format(d_xiaoji);
            viewHolder.value_xiaoji.setText(s);

//  System.out.println(d_num+"        "+d_value+"        "+d_xiaoji);
    //  String s=String.valueOf(d_xiaoji);
    //  System.out.println(s);
}
        if(data.thingbuycar_num!="1"){
            viewHolder.image_reduce.setImageResource(R.drawable.shopping_cart_product_num_reduce);
        }
        viewHolder.image_add.setOnClickListener(new View.OnClickListener(){         
            @Override
            public void onClick(View v) {
                viewHolder.image_reduce.setImageResource(R.drawable.shopping_cart_product_num_reduce);
//              double d_value=Double.parseDouble(data.thingbuycar_value);
//              double d_num=Double.parseDouble(data.thingbuycar_num);
//              double d_xiaoji=d_value*d_num/1.00;
//              DecimalFormat df = new DecimalFormat("0.00");
//              String s=df.format(d_xiaoji);
                viewHolder.value_xiaoji.setText("5");
                        }});
        viewHolder.image_reduce.setOnClickListener(mylistener_reduce);
        viewHolder.image_change.setOnClickListener(mylistener_change);
        return convertView;
    }
    private class viewHolder{
        private ImageView image;
        private TextView name;
        private TextView value;
        private TextView num;
        private TextView value_xiaoji;
        private ImageView image_change;
        private ImageView image_reduce;
        private ImageView image_add;
    }
    private class MyListener implements OnClickListener{
        int mposition;
        int change;
        public MyListener(int mposition,int change){
            this.mposition=mposition;
            this.change=change;
        }
        public void updata_xiaoji(int mposition){
            viewHolder.value_xiaoji.setTag(mposition);
            Log.i("wu", "真的"+viewHolder.value_xiaoji.getTag());
            double d_value=Double.parseDouble(mlist.get(mposition).thingbuycar_value);
            double d_num=Double.parseDouble(mlist.get(mposition).thingbuycar_num);
            double d_xiaoji=d_value*d_num/1.00;
            DecimalFormat df = new DecimalFormat("0.00");
            String s=df.format(d_xiaoji);
            viewHolder.value_xiaoji.setText(s);
        }
        @Override
        public void onClick(View v) {
            String Snum=null;
            int mnumber=(int)Double.parseDouble(mlist.get(mposition).thingbuycar_num);
            switch (change) {
            //加
            case 1:
                viewHolder.image_add.setTag(mposition);
                viewHolder.image_change.setTag(mposition);
                viewHolder.image_reduce.setTag(mposition);
                viewHolder.image_reduce.setImageResource(R.drawable.shopping_cart_product_num_reduce);
                mnumber++;
                 Snum=""+mnumber;
                 mlist.get(mposition).thingbuycar_num=Snum;
             viewHolder.num.setText(Snum);
             Log.i("wu",  ""+viewHolder.image_add.getTag());
                updata_xiaoji(mposition);
                Log.i("wu", ""+mposition);
                break;
            case 2:
                //减法
                viewHolder.image_add.setTag(mposition);
                viewHolder.image_change.setTag(mposition);
                viewHolder.image_reduce.setTag(mposition);
                if(mnumber>1){
                mnumber--;
                if(mnumber==1){
                    viewHolder.image_reduce.setImageResource(R.drawable.shopping_cart_product_num_reduce_disable);
                }
                 Snum=""+mnumber;
                 mlist.get(mposition).thingbuycar_num=Snum;
                 viewHolder.num.setText(Snum);
                }
                updata_xiaoji(mposition);
                Log.i("wu", ""+mposition);
                break;
            case 3:
                //默认false  小计
                viewHolder.image_add.setTag(mposition);
                viewHolder.image_change.setTag(mposition);
                viewHolder.image_reduce.setTag(mposition);
                if(mlist.get(mposition).thingbuycar_or){
                    viewHolder.image_change.setImageResource(R.drawable.pay_and_delivery_fast_checked);
                    updata_xiaoji(mposition);
                    mlist.get(mposition).thingbuycar_or=false;
                }else{![图片说明](https://img-ask.csdn.net/upload/201510/22/1445514565_269498.png)
                    viewHolder.image_change.setImageResource(R.drawable.pay_and_delivery_unchecked);
                    viewHolder.value_xiaoji.setText("0.00");
                    mlist.get(mposition).thingbuycar_or=true;
                }
                break;
            }
        }

    }
}
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • cxlovu 2015-10-22 11:54
    已采纳

    我以前项目里的,借你参考一下,不难的

    public class ShopItemAdapter extends BaseAdapter{
    
        private Context context;                        //运行上下文  
        private List<Map<String, Object>> listItems;    ///商品信息集合  
        private LayoutInflater listContainer;           //视图容器
        private Integer []num;
        private Handler mHandler;
    
        public final class ListItemView{                //自定义控件集合   
    
            public ImageView product;
            public TextView pro_name;
            public TextView shelves_date;
            public TextView make_date;
            public TextView use_date;
            public TextView new_price;
            public TextView old_price;
            public TextView reduce;
            public TextView num;
            public TextView add;
            public TextView delete;
     } 
        private ListItemView  listItemView;
    
        public ShopItemAdapter(Context context,
                List<Map<String, Object>> listItems, Integer[] from,
                Handler mHandler) {
            // TODO Auto-generated constructor stub
    
             this.context = context;            
             listContainer = (LayoutInflater)context.getSystemService(
                     Context.LAYOUT_INFLATER_SERVICE);  
             this.listItems = listItems; 
             this.num = new Integer[from.length];
             System.arraycopy(from, 0, num, 0, from.length);
             this.mHandler = mHandler;
        }
    
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return listItems.size(); 
        }
    
        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return listItems.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
    
        /**  
         * ListView Item设置  
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
             Log.e("method", "getView");   
                final int selectID = position;   
                //自定义视图  
                if (convertView == null) {   
                    listItemView = new ListItemView();    
                    //获取list_item布局文件的视图 
                    convertView = listContainer.inflate(R.layout.item, null);   
                    //获取控件对象   
                    listItemView.product = (ImageView)convertView.findViewById(R.id.pro_pic);
                    listItemView.pro_name = (TextView)convertView.findViewById(R.id.pro_name);
                    listItemView.shelves_date = (TextView)convertView.findViewById(R.id.shelves_date);
                    listItemView.make_date = (TextView)convertView.findViewById(R.id.make_date);
                    listItemView.use_date = (TextView)convertView.findViewById(R.id.use_date);
                    listItemView.new_price = (TextView)convertView.findViewById(R.id.new_price);
                    listItemView.old_price = (TextView)convertView.findViewById(R.id.old_price);
                    listItemView.reduce = (TextView)convertView.findViewById(R.id.reduce);
                    listItemView.num = (TextView)convertView.findViewById(R.id.num);
                    listItemView.add = (TextView)convertView.findViewById(R.id.add);
                    listItemView.delete = (TextView)convertView.findViewById(R.id.delete);
    
                    listItemView.reduce.setOnClickListener(new lvButtonListener(position));
                    listItemView.add.setOnClickListener(new lvButtonListener(position));
                    listItemView.delete.setOnClickListener(new lvButtonListener(position));
    
    
                  //设置控件集到convertView    
                    convertView.setTag(listItemView);   
                }
                else {   
                    listItemView = (ListItemView)convertView.getTag();   
    
                }
    
    
              //设置文字和图片    
    
                listItemView.pro_name.setText((String) listItems.get(position)   
                        .get("info"));
                listItemView.shelves_date.setText((String) listItems.get(position)   
                        .get("saleTime"));
                listItemView.make_date.setText((String) listItems.get(position)   
                        .get("produceTime"));
                listItemView.use_date.setText((String) listItems.get(position)   
                        .get("durabilityPeriod"));
                listItemView.use_date.setText((String) listItems.get(position)   
                        .get("durabilityPeriod"));
                listItemView.num.setText(String.valueOf(listItems.get(position)
                        .get("num")));
                listItemView.new_price.setText("¥"+String.valueOf(listItems.get(position)
                        .get("new_price")));
                listItemView.old_price.setText("¥"+String.valueOf(listItems.get(position)
                        .get("old_price")));
                listItemView.old_price.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG);
                return convertView;
        }
    
    
          public void addNum(int position){
            int num=(Integer) listItems.get(position).get("num");
            num++;
            listItems.get(position).put("num",num);
            mHandler.sendMessage(mHandler.obtainMessage(1, getTotalPrice()));
            this.notifyDataSetChanged();
          }
    
          public void cutNum(int position){
            int num = (Integer) listItems.get(position).get("num");
            if(num>0){
              num--;
            }
    
            //将数据源中改变后的数据重新放进数据源中,再加载到item中
            listItems.get(position).put("num",num);
            mHandler.sendMessage(mHandler.obtainMessage(1, getTotalPrice()));
          //重新刷新页面
            this.notifyDataSetChanged();
          }
    
          public void deletItem(int position){
              listItems.remove(position);
              mHandler.sendMessage(mHandler.obtainMessage(1, getTotalPrice()));
              //重新刷新页面
                this.notifyDataSetChanged();      
          }
    
          /**
             * 计算选中商品的金额
             * @return  返回需要付费的总金额
             */
            private Integer getTotalPrice(){
    
                Integer totalPrice = 0;
                for(int i=0;i<listItems.size();i++){
                     int num = (Integer) listItems.get(i).get("num");
                     int price = (Integer) listItems.get(i).get("new_price");
                     totalPrice += num * price;
                }
                return totalPrice;
            }
    
    
        //按钮监听事件,实现view中的监听事件
        class lvButtonListener implements  View.OnClickListener{
            private int position;
            lvButtonListener(int pos){
              position = pos;
            }
            @Override
            public void onClick(View v){
              int vid = v.getId();
              if(vid == listItemView.add.getId()){
                addNum(position);
              }
              else if(vid == listItemView.reduce.getId()){
                cutNum(position);
              }
              else if(vid == listItemView.delete.getId()){
                  deletItem(position);
              }
    
            }
          }
    }
    
    
    打赏 评论
  • 农村大代表_ 2015-10-23 01:09

    if(data.thingbuycar_num!="1"){
    viewHolder.image_reduce.setImageResource(R.drawable.shopping_cart_product_num_reduce);
    }

    把else加上,使用holder之后,带状态的都需要成对出现,不能只出现if.

    打赏 评论

相关推荐 更多相似问题