在Listview中实现checkBox多选并在activity中取到选中的值
来源:互联网 发布:大数据和计算思维 编辑:程序博客网 时间:2024/06/07 17:05
重写一个自己的adapter,实现带checkbox的列表(这里用到了ViewHolder,好控制,且运行的次数也少提高了效率)
public class QW_push_listAdapter extends BaseAdapter{ Context context; List<HashMap<String,Object>> datas; public static HashMap<Integer,Boolean> isSelected; private LayoutInflater inflater = null; public QW_push_listAdapter(Context context,List<HashMap<String,Object>> datas) {//System.out.println("---in adapter----");this.context = context;this.datas = datas;inflater = LayoutInflater.from(context);isSelected = new HashMap<Integer, Boolean>();//给初始状态for(int i = 0;i<datas.size();i++){//System.out.println("states:"+(Boolean)datas.get(i).get("state"));isSelected.put(i, (Boolean)datas.get(i).get("state"));}}@Overridepublic int getCount() {return datas.size();}@Overridepublic Object getItem(int position) {return datas.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(final int position, View convertView, ViewGroup parent) {ViewHolder holder = null;if(convertView == null){//System.out.println("---getview ---: position == :"+position);holder = new ViewHolder();convertView = inflater.inflate(R.layout.qw_push_item, null);holder.tv = (TextView) convertView.findViewById(R.id.qw_push_item_text);holder.iv = (ImageView) convertView.findViewById(R.id.qw_push_item_image);holder.cb = (CheckBox) convertView.findViewById(R.id.qw_push_item_checkBox);//为view设置标签convertView.setTag(holder);}else{holder = (ViewHolder)convertView.getTag();}//System.out.println("(String)datas.get(position).get('textview')"+(String)datas.get(position).get("textview"));holder.tv.setText((String)datas.get(position).get("textview"));//System.out.println("image:"+Integer.parseInt( datas.get(position).get("imageview").toString()));holder.iv.setBackgroundResource(Integer.parseInt(datas.get(position).get("imageview").toString()));//System.out.println("isselected"+isSelected.get(position));//System.out.println("---set states:---"+isSelected.get(position));if(isSelected.get(position)){holder.cb.setChecked(true);}else{holder.cb.setChecked(false);}holder.cb.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {// TODO Auto-generated method stubif (isChecked) {//System.out.println("--in in checked : ischecked:"+position);isSelected.put(position, isChecked);} else {isSelected.put(position,isChecked);}//buttonView.setChecked((isSelected.get(position) == null ? false : true));buttonView.setChecked(isSelected.get(position));}});return convertView;}public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {QW_push_listAdapter.isSelected = isSelected;}}
写一个自己的viewHolder类
public class ViewHolder {CheckBox cb;TextView tv;ImageView iv;RadioButton rb;}
Activity中的oncreat()方法中实现checkbox多选
Boolean states[]={true,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,true};@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.qw_push);//设置标题System.out.println("-----on push xml---");title_top_tv = (TextView)findViewById(R.id.title_center_title);title_top_tv.setText("推送设置");title_top_back = (Button)findViewById(R.id.qw_title_left_back);title_top_back.setBackgroundResource(R.drawable.qw_left_back);title_top_save = (Button)findViewById(R.id.qw_title_right_button);title_top_save.setBackgroundResource(R.drawable.qw_right_save);title_top_save.setVisibility(0);layout = (LinearLayout)findViewById(R.id.qw_push_layout);push_listView = (ListView)findViewById(R.id.qw_push_list);//设置手势滑动myGestureDetector = new GestureDetector(this);layout.setOnTouchListener(this);layout.setLongClickable(true);//点击“返回”时退出当前的页title_top_back.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubQW_PushActivity.this.finish();}});//点击“保存”事件title_top_save.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubsavefile();Toast.makeText(QW_PushActivity.this, "保存成功", Toast.LENGTH_SHORT).show();}});//数据源、适配器,对列表进行操作String temp_text[] ={"内部消息","个人资料","日程管理","周程","工作日报"}; int temp_image[] = {R.drawable.qw_neibuxiaoxi,R.drawable.qw_gerenzhiliao,R.drawable.qw_richengguanli,R.drawable.qw_zhoubaoguanli,R.drawable.qw_gongzuoribao,};List<HashMap<String,Object>> datas = new ArrayList<HashMap<String,Object>>();for(int i = 0;i<temp_text.length;i++){HashMap<String,Object> map = new HashMap<String, Object>();map.put("textview", temp_text[i]);map.put("imageview", temp_image[i]);map.put("state", states[i]);datas.add(map);}list_adapter = new QW_push_listAdapter(this,datas);push_listView.setAdapter(list_adapter);}//end oncreatprivate HashMap<Integer, Boolean> state;//保存public void savefile(){state =list_adapter.isSelected;String options="选择的项是:";for(int j=0;j<list_adapter.getCount();j++){//System.out.println("state.get("+j+")=="+state.get(j));if(state.get(j)!=null && state.get(j)== true ){@SuppressWarnings("unchecked")HashMap<String, Object> map=(HashMap<String, Object>) list_adapter.getItem(j);String username=map.get("textview").toString(); //得到更改后的值//String id=map.get("friend_id").toString();options+="\n"+"."+username;System.out.println("----save-----"+options);}Toast.makeText(QW_PushActivity.this, "保存成功", Toast.LENGTH_SHORT).show();QW_PushActivity.this.finish();}}
item的xml文件:
<CheckBox android:id="@+id/qw_push_item_checkBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="80dp" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:focusable="false" //不可获得焦点 android:clickable="false" //不可点击 android:focusableInTouchMode="false"//通过touch来获得焦点 />
- 在Listview中实现checkBox多选并在activity中取到选中的值
- CheckBox在listview中实现单选
- Android checkBox 在listView 实现单选,并记录保存
- flex在DataGrid中添加checkbox复选框,并获取选中的值
- 在gridview中获取选中的CheckBox
- 如何在含有checkbox的listview条目中获取到选中的条目信息
- 在GridView中加入CheckBox实现全选以及提交CheckBox选中的相关内容
- JS实现对gridview中的checkbox的选中个数记录,并在页面刷新时保持checkbox的状态 .
- ListView的bug解决,如果有checkbox的话,选中的值在翻屏时会跳来跳去
- html中checkbox在GridView选中并取值方法(转载)
- html中的checkbox如何在GridView选中并取值
- Android开发:在ListView实现多选项CheckBox的例子
- 关于listview中checkbox的选中问题
- gridview、listview适配器布局中带有checkbox实现的列表 checkbox只能有一项选中
- android-如何在ListView中显示单选按钮并能默认选中
- 在js中怎样获得checkbox里选中的多个值?
- 在js中怎样获得checkbox里选中的多个值?
- 在js中怎样获得checkbox里选中的多个值
- 解读C指针(1)——内存与地址
- 解读C指针(2)——类型说明
- (转)C# (线程安全的哈希表)Hashtable Synchronized vs SyncRoot
- 解读C指针(3)——深入理解
- Excel Old format or invalid type library 错误原因
- 在Listview中实现checkBox多选并在activity中取到选中的值
- 2007Word中,删除一个题注后,如何批量更新编号?
- 状态模式(State Pattern)
- Unity 控制游戏对象,显示小地图
- This text field does not specify an inputType or a hint
- [LeetCode 116 117]
- 15 C/C++中的日期和時間 time_t與struct tm轉換
- LIBCD.lib(wincrt0.obj) : error LNK2001
- IIS6.0的配置保存与恢复