在Listview中实现checkBox多选并在activity中取到选中的值

重写一个自己的adapter,实现带checkbox的列表(这里用到了ViewHolder,好控制,且运行的次数也少提高了效率)

public class QW_push_listAdapter extends BaseAdapter{
	 Context context;
	 List> datas;
	 public static HashMap isSelected;
	 private LayoutInflater inflater = null;
	
	 
	
	public QW_push_listAdapter(Context context,List> datas) {
		//System.out.println("---in adapter----");
		this.context = context;
		this.datas = datas;
		inflater = LayoutInflater.from(context);
		isSelected = new HashMap();
		//给初始状态
		for(int i = 0;i 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};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.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() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				QW_PushActivity.this.finish();
			}
		});
		//点击“保存”事件
		title_top_save.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				savefile();
				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> datas = new ArrayList>();
		for(int i = 0;i map = new HashMap();
			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 oncreat
	
	private HashMap state;
	
	//保存
	public void savefile(){
		state =list_adapter.isSelected;
		String options="选择的项是:";
		for(int j=0;j map=(HashMap) 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文件:

 
  
 
  
 
  
 
  
 
 

你可能感兴趣的:(在Listview中实现checkBox多选并在activity中取到选中的值)