Android ListView 中的CheckBox点击乱系

本文介绍了一种基于Android平台的自定义列表适配器实现方法,通过使用BaseAdapter来展示动态数据列表,并实现了CheckBox的选择功能及价格总计计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.commons.widget.NetworkImageView;
import com.geluyawangluo.withthenight.R;

import java.util.ArrayList;
import java.util.HashMap;


public class CatAdapter extends BaseAdapter {
   private Context activity;
   private ArrayList<HashMap<String,String>> list;
   private TextView allPrice;
   private float allp=0;
   private ViewHolder holder;
   private ArrayList<Boolean> booList;




   public CatAdapter(Context activity,  ArrayList<HashMap<String,String>> list, TextView allPrice,ArrayList<Boolean> booList) {
      this.activity = activity;
      this.list = list;
      this.allPrice=allPrice;
      this.booList=booList;
      allp=0;
   }

   @Override
   public int getCount() {
      // TODO Auto-generated method stub
      return list.size();
//    return 5;
   }


   @Override
   public Object getItem(int position) {
      // TODO Auto-generated method stub
      return null;
   }

   @Override
   public long getItemId(int position) {
      // TODO Auto-generated method stub
      return position;
   }

   @Override
   public View getView(final int position, View convertView, ViewGroup parent) {
       holder = null;
      if (convertView == null) {
         holder = new ViewHolder();
          convertView=((Activity) activity).getLayoutInflater().inflate(R.layout.layout_catlist,null);
         holder.nameTv = (TextView) convertView.findViewById(R.id.name);
         holder.image=(NetworkImageView) convertView.findViewById(R.id.image);
         holder.price = (TextView) convertView.findViewById(R.id.jiage);
         holder.num = (TextView) convertView.findViewById(R.id.shuliang);
         holder.box=(CheckBox)convertView.findViewById(R.id.box);
         holder.text1=(TextView)convertView.findViewById(R.id.text1);
         holder.text2=(TextView)convertView.findViewById(R.id.text2);
         holder.text3=(TextView)convertView.findViewById(R.id.text3);
         holder.text4=(TextView)convertView.findViewById(R.id.text4);
         holder.jiaBtn=(ImageView)convertView.findViewById(R.id.jiabtn);
         holder.jianBtn=(ImageView)convertView.findViewById(R.id.jianbtn);

         convertView.setTag(holder);
      } else {
         holder = (ViewHolder) convertView.getTag();
      }

      holder.nameTv.setText(list.get(position).get("name"));
      holder.price.setText(list.get(position).get("price"));
      holder.image.setImageUri(list.get(position).get("img"));
      holder.num.setText(list.get(position).get("count"));
      holder.price.setText(list.get(position).get("price"));
      holder.text2.setText(list.get(position).get("shuxin2"));
      holder.text4.setText(list.get(position).get("shuxin1"));
      
      final float s=Float.valueOf(list.get(position).get("price"));
      final int nums= Integer.valueOf(list.get(position).get("count"));
      final float i=s*nums;
      holder.box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                               if(isChecked){

                              booList.set(position,true);

                              allp+=i;
                                   }else{
                              booList.set(position,false);
                              allp-=i;
                                  }
                        allPrice.setText("¥"+allp+"");
                            }
                      });


                  if(booList.get(position)==true){
                  holder.box.setChecked(true);
                  allp+=i;
                     }else{
                  holder.box.setChecked(false);
                  allp-=i;
                  if(allp<0){
                     allp=0;
                  }

                      }
      allPrice.setText("¥"+allp+"");


      // TODO Auto-generated method stub
      return convertView;
   }

   public class ViewHolder {
      TextView nameTv;
      NetworkImageView image;
      TextView price;
      TextView num;
      CheckBox box;
      TextView text1;
      TextView text2;
      TextView text3;
      TextView text4;
      ImageView jiaBtn;
      ImageView jianBtn;
      
      
      

   }

    class A {

             public static final int TYPE_CHECKED = 1;
              public static final int TYPE_NOCHECKED = 0;


             int type;

                    public A(int type){

                     this.type = type;
                 }
           }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值