listView的item中有checkbox,重复被选中的问题

当选中第一个的时候,底下的也被默认选中了

解决方案:


public class ListHuowuDetaikAdapter extends BaseAdapter {
 private LayoutInflater inflater;
 private Context context;
 private ArrayList mallShop=new ArrayList();
 private ImageLoader loader;
 public ArrayList checkmall = new ArrayList();
 //将状态用HashMap保存起来
 private HashMap checkpos = new HashMap();
 //记录checkBox的选中状态
 public void setData(ArrayList mallShop){
  this.mallShop = mallShop;
  for (int i = 0; i < mallShop.size(); i++) {
   checkpos.put(i, false);
  }
 }
 public ListHuowuDetaikAdapter(Context context) {   
  this.context = context;
  inflater = LayoutInflater.from(context);
  loader = myImg.initImg(context); 
 }
 @Override
 public int getCount() {
  // TODO Auto-generated method stub
  return mallShop!=null? mallShop.size():0;
 }
 @Override
 public Object getItem(int arg0) {
  // TODO Auto-generated method stub
  return mallShop.get(arg0);
 }
 @Override
 public long getItemId(int arg0) {
  // TODO Auto-generated method stub
  return arg0;
 }
 @Override
 public View getView(final int pos, View arg1, ViewGroup arg2) {
  // TODO Auto-generated method stub
  final ViewHolder holder;
  if (arg1 == null) {
   holder = new ViewHolder();
   arg1 = inflater.inflate(R.layout.list_huoqu_detail, null);
   holder.img_yijianpic = (ImageView) arg1.findViewById(R.id.img_yijianpic);
   holder.rad_zhuangtai = (CheckBox) arg1.findViewById(R.id.rad_zhuangtai);
   holder.tv_pinpai_name = (TextView) arg1.findViewById(R.id.tv_pinpai_name);
   holder.tv_yijian_huo_name = (TextView) arg1.findViewById(R.id.tv_yijian_huo_name);
   holder.tv_yijian_huo_price = (TextView) arg1.findViewById(R.id.tv_yijian_huo_price);   
   arg1.setTag(holder);
  } else {
   holder = (ViewHolder) arg1.getTag();
  }  
   holder.tv_yijian_huo_name.setText(mallShop.get(pos).getName());
  try {
   loader.displayImage(mallShop.get(pos).getShop_image()!=null?mallShop.get(pos).getShop_image().get(0):"",holder.img_yijianpic); 
  } catch (Exception e) {
  }
     holder.tv_yijian_huo_price.setText("¥"+mallShop.get(pos).getPrice()+"元");
  holder.tv_pinpai_name.setText(mallShop.get(pos).getBrand());
  holder.rad_zhuangtai.setOnCheckedChangeListener(new OnCheckedChangeListener() {   
   @Override
   public void onCheckedChanged( CompoundButton buttonView, boolean isChecked) {
    if(isChecked){
     checkmall.add(mallShop.get(pos));
     checkpos.put(pos,true);
    }else{
     checkmall.remove(mallShop.get(pos));
     checkpos.put(pos, false);
    }
   }    
   });
  holder.rad_zhuangtai.setChecked(checkpos.get(pos));
  return arg1;}
 class ViewHolder {
  ImageView img_yijianpic;
  TextView tv_yijian_huo_name, tv_yijian_huo_price, tv_pinpai_name;
  CheckBox rad_zhuangtai;
 }

在Activity中发送请求获取商品的信息


public class YiJianShanghuoActivity extends Activity implements OnClickListener {

 private ListView list_huoqu_detail;
 private ListHuowuDetaikAdapter adapter;
 private ImageView img_yijian;
 ArrayList mallShop = new ArrayList();

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  setContentView(R.layout.activity_yi_jian_shanghuo);

  list_huoqu_detail = (ListView) findViewById(R.id.list_huoqu_detail);
  img_yijian = (ImageView) findViewById(R.id.img_yijian);
  img_yijian.setOnClickListener(this);
  adapter = new ListHuowuDetaikAdapter(this);
  list_huoqu_detail.setAdapter(adapter);
  HttpUtils utils = new HttpUtils();
  utils.send(HttpMethod.POST, TaskApi.ip + TaskApi.selectMallShop, new RequestCallBack() {

   @Override
   public void onFailure(HttpException arg0, String arg1) {
    // TODO Auto-generated method stub
    Log.e("_MYLOG_", "ListHuowuDetaikAdapter----" + arg0.toString() + "...\n" + arg1.toString());
    Toast.makeText(YiJianShanghuoActivity.this, "网络访问失败", Toast.LENGTH_SHORT).show();
   }

   @Override
   public void onSuccess(ResponseInfo arg0) {
    // TODO Auto-generated method stub
    Log.e("_MYLOG_", "selectMallShop" + arg0.result.toString());
    Gson gson = new Gson();
    AllShop as = gson.fromJson(arg0.result, AllShop.class);
    mallShop = as.getMallShop();
    adapter.setData(mallShop);
    adapter.notifyDataSetChanged();

   }
  });

 }

 @Override
 public void onClick(View arg0) {
  // TODO Auto-generated method stub
  switch (arg0.getId()) {
  case R.id.img_yijian:
   addToshop();
   finish();
   break;

  default:
   break;
  }
 }

 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
  // TODO Auto-generated method stub
  switch (keyCode) {
  case KeyEvent.KEYCODE_BACK:
   addToshop();
   break;

  }
  return super.onKeyDown(keyCode, event);
 }

 private void addToshop() {
  // TODO Auto-generated method stub
  HttpUtils utils = new HttpUtils();
  for (int i = 0; i < adapter.checkmall.size(); i++) {

   RequestParams params = new RequestParams();
   params.addBodyParameter("username", Shared.login_username);
   params.addBodyParameter("barcode", adapter.checkmall.get(i).getBarcode());
   params.addBodyParameter("name", adapter.checkmall.get(i).getName());
   params.addBodyParameter("brand", adapter.checkmall.get(i).getBrand());
   params.addBodyParameter("original_price", adapter.checkmall.get(i).getPrice());
   params.addBodyParameter("shop_one_type", adapter.checkmall.get(i).getShop_one_type());
   params.addBodyParameter("shop_two_type", adapter.checkmall.get(i).getShop_two_type());
   params.addBodyParameter("description", adapter.checkmall.get(i).getDescrip());
   try {
    params.addBodyParameter("shop_image", adapter.checkmall.get(i).getShop_image().get(0));
   } catch (Exception e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   }
   utils.send(HttpMethod.POST, TaskApi.ip + TaskApi.addSellerShopFromMall, params,
     new RequestCallBack() {

      @Override
      public void onFailure(HttpException arg0, String arg1) {
       // TODO Auto-generated method stub
      }

      @Override
      public void onSuccess(ResponseInfo arg0) {
       // TODO Auto-generated method stub
       JSONObject json;
       Log.e("_MYLOG_", "addSellerShopFromMall" + arg0.result.toString());
       try {
        json = new JSONObject(arg0.result);
        if (!json.getString("result").equals("success")) {
        }
       } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }

      }
     });
  }
 }




你可能感兴趣的:(listView的item中有checkbox,重复被选中的问题)