关闭

关于listview的单选和多选

184人阅读 评论(0) 收藏 举报


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里写监听控件


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:326次
    • 积分:23
    • 等级:
    • 排名:千里之外
    • 原创:2篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    文章存档