关于listview的单选和多选


nameAdapter=new adapter(MainActivity.this, list,listSelect);


adapter里

public adapter(Context context,List<String> listdata,List<Map<String, String>> listSelect) {
   // TODO Auto-generated constructor stub
   this.context=context;
   this.listdata=listdata;
   this.listSelect=listSelect;  //判断状态的集合
  }

holder.tName.setText(listdata.get(position));
   String state=listSelect.get(position).get("state");
   if (state.equals("0")) {
    holder.iState.setImageResource(R.drawable.home_socket_time_add_equipment_off);
   }else if (state.equals("1")) {
    holder.iState.setImageResource(R.drawable.home_socket_time_add_equipment_on);
   }


listView.setOnItemClickListener(new OnItemClickListener() {

   @Override
   public void onItemClick(AdapterView<?> arg0, View arg1, int position,
     long arg3) {
    //多选
    String s=listSelect.get(position).get("state");
    if (s.equals("0")) {
     Map<String, String> map=new HashMap<String, String>();
     map.put("state", "1");
     listSelect.set(position, map);
    }else if (s.equals("1")) {
     Map<String, String> map=new HashMap<String, String>();
     map.put("state", "0");
     listSelect.set(position, map);
    }
    nameAdapter.notifyDataSetChanged();
    System.out.println(listSelect);
    for (int i = 0; i < listSelect.size(); i++) {
     String ss=listSelect.get(i).get("state");
     if (ss.equals("1")) {
      Toast.makeText(MainActivity.this, "第"+(i+1)+"被选中", Toast.LENGTH_SHORT).show();
     }
    }
    
    //单选

 //首先初始化state的值  把上次点击的改为未点击
   /* for (int i = 0; i < listSelect.size(); i++) {
     
     Map<String, String> map=new HashMap<String, String>();
     map.put("state", "0");
     listSelect.set(i, map);
    }
    System.out.println("初始==="+listSelect);
    String s=listSelect.get(position).get("state");
    if (s.equals("0")) {
     Map<String, String> map1=new HashMap<String, String>();
     map1.put("state", "1");
     listSelect.set(position, map1);
    }else if (s.equals("1")) {
     Map<String, String> map1=new HashMap<String, String>();
     map1.put("state", "0");
     listSelect.set(position, map1);
    }
    System.out.println("点击后==="+listSelect);
    nameAdapter.notifyDataSetChanged();
    String ss=listSelect.get(position).get("state");
    if (ss.equals("1")) {
     Toast.makeText(MainActivity.this, "第"+(position+1)+"被选中",Toast.LENGTH_SHORT).show();
    }*/
   }
  });

重点是list.set(position,map)的使用

通过listview.onitemclick的监听来改变状态   如果是点击item里的控件来改变状态 就在adapter里写监听控件


阅读更多
想对作者说点什么? 我来说一句

ListView实现单选多选

2014年11月01日 2.63MB 下载

没有更多推荐了,返回首页

不良信息举报

关于listview的单选和多选

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭