ListView

/**
         * 模拟数据
         */
        private void initPersonData(){
            GoodsMainInformation goodsMainInformation;
            for(int i=1;i<=goodslist.size();i++){
                goodsMainInformation = new GoodsMainInformation();
                goodsMainInformation.setGoodname(goodsName[i-1]+" "+goodCode[i-1]);
                goodsMainInformation.setGoodPrice( Float.toString(goodsPrice[i-1])+"RMB");
                goodsInformation.add(goodsMainInformation);

            }
        }        
        //自定义ListView适配器
        class MyListAdapter extends BaseAdapter{
            List<Boolean> mChecked;
            List<GoodsMainInformation> listGoods;
            HashMap<Integer,View> map = new HashMap<Integer,View>();             
            public MyListAdapter(List<GoodsMainInformation> list){
                listGoods = new ArrayList<GoodsMainInformation>();
                listGoods = list;                
                mChecked = new ArrayList<Boolean>();
                for(int i=0;i<list.size();i++){
                    mChecked.add(false);
                }
            }    
            @Override
            public int getCount() {
                return listGoods.size();
            } 
            @Override
            public Object getItem(int position) {
                return listGoods.get(position);
            }    
            @Override
            public long getItemId(int position) {
                return position;
            }    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view;
                ViewHolder holder = null;                
                if (map.get(position) == null) {
                    Log.e("MainActivity","position1 = "+position);

                    LayoutInflater mInflater = (LayoutInflater) mContext
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    view = mInflater.inflate(R.layout.listitem, null);
                    holder = new ViewHolder();
                    holder.selected = (CheckBox)view.findViewById(R.id.list_select);
                    holder.name = (TextView)view.findViewById(R.id.list_name);
                    holder.price = (TextView)view.findViewById(R.id.list_price);
                    final int p = position;
                    map.put(position, view);
                    holder.selected.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            CheckBox cb = (CheckBox)v;
                            mChecked.set(p, cb.isChecked());
                        }
                    });
                    view.setTag(holder);
                }else{
                    Log.e("MainActivity","position2 = "+position);
                    view = map.get(position);
                    holder = (ViewHolder)view.getTag();
                }

                holder.selected.setChecked(mChecked.get(position));
                holder.name.setText(listGoods.get(position).getGoodname());
                holder.price.setText(listGoods.get(position).getGoodPrice());

                return view;
            }            
        }        
        static class ViewHolder{
            CheckBox selected;
            TextView name;
            TextView price;
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值