程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> listview適配器監聽-購物車listview加減按鈕監聽

listview適配器監聽-購物車listview加減按鈕監聽

編輯:編程綜合問答
購物車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{![圖片說明](http://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;
            }
        }

    }
}

最佳回答:


我以前項目裡的,借你參考一下,不難的

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);
          }

        }
      }
}

qq_28809119
qq_28809119
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved